- macro_rules
- expr
- "*"
- "+"
Macro with many parameters to say hello
- This macro can accept 0 or more parameters and then it will repeate the code as many times as parameters we got.
- Instead of * we could use + in the declaration and that would mean the macro accepts 1 or more parameters.
examples/macros/say-hello-many-times/src/main.rs
macro_rules! say_hello { ($( $name: expr ),*) => { $( println!("Hello {}!", $name); )* }; } fn main() { println!("----"); say_hello!(); println!("----"); say_hello!("Foo", "Bar", "Jane", "Joe"); // generates code with 4 print statements println!("----"); say_hello!("Rust", "Perl", "Python"); // generates code with 3 print statements println!("----"); }
---- ---- Hello Foo! Hello Bar! Hello Jane! Hello Joe! ---- Hello Rust! Hello Perl! Hello Python! ----