You never explained what the problem was with your existing code, so I don't know what to highlight in this example:
macro_rules! foo {
($($($x:expr),*);*) => {
$(
$(
print!("{},", $x);
)*
println!("semi");
)*
}
}
fn main() {
foo!(1,2,3;4,5,6;7,8,9);
}
I can point out things from your original code:
- It's called
macro_rules!
, not macro_rule!
- The name of the macro being defined goes before the original
{
, not after.
- Like most programming, paired delimiters need to be evenly matched to be syntactically valid.
The Rust Programming Language, first edition has several pieces of valuable information.
Basic syntax for defining a macro is covered in the macros chapter; I strongly suggest you read the entire thing. It also links to the reference, which contains some more lower-level detail.
The section most related to your question is:
The repetition operator follows two principal rules:
$(...)*
walks through one "layer" of repetitions, for all of the $names
it contains, in lockstep, and
- each
$name
must be under at least as many $(...)*
s as it was matched against. If it is under more, it’ll be duplicated, as appropriate.