macros and stuff

This commit is contained in:
Ivica Šimić 2019-07-18 12:59:46 +02:00
parent 9bdf1995ef
commit aeaa001b38
6 changed files with 12 additions and 8 deletions

View file

@ -8,7 +8,7 @@ macro_rules! my_macro {
} }
fn main() { fn main() {
my_macro(); my_macro!();
} }

View file

@ -1,17 +1,15 @@
// macros2.rs // macros2.rs
// Make me compile! Scroll down for hints :) // Make me compile! Scroll down for hints :)
fn main() {
my_macro!();
}
macro_rules! my_macro { macro_rules! my_macro {
() => { () => {
println!("Check out my macro!"); println!("Check out my macro!");
}; };
} }
fn main() {
my_macro!();
}

View file

@ -1,6 +1,7 @@
// macros3.rs // macros3.rs
// Make me compile, without taking the macro out of the module! Scroll down for hints :) // Make me compile, without taking the macro out of the module! Scroll down for hints :)
#[macro_use]
mod macros { mod macros {
macro_rules! my_macro { macro_rules! my_macro {
() => { () => {

View file

@ -4,7 +4,7 @@
macro_rules! my_macro { macro_rules! my_macro {
() => { () => {
println!("Check out my macro!"); println!("Check out my macro!");
} };
($val:expr) => { ($val:expr) => {
println!("Look at this other macro: {}", $val); println!("Look at this other macro: {}", $val);
} }

View file

@ -4,7 +4,7 @@
fn main() { fn main() {
let vec0 = Vec::new(); let vec0 = Vec::new();
let vec1 = fill_vec(vec0); let mut vec1 = fill_vec(vec0);
println!("{} has length {} content `{:?}`", "vec1", vec1.len(), vec1); println!("{} has length {} content `{:?}`", "vec1", vec1.len(), vec1);

View file

@ -5,6 +5,11 @@
// Write a macro that passes the test! No hints this time, you can do it! // Write a macro that passes the test! No hints this time, you can do it!
macro_rules! my_macro {
($var:expr) => { //or just ("world!")
"Hello world!"
}
}
fn main() { fn main() {
if my_macro!("world!") != "Hello world!" { if my_macro!("world!") != "Hello world!" {
panic!("Oh no! Wrong output!"); panic!("Oh no! Wrong output!");