November 2017
Intermediate to advanced
264 pages
5h 45m
English
What if there is more than one argument? Then we can enclose the pattern with a $(...)*, where * means zero or more (instead of *, you can use + which means one or more).
For example, the following macro, printall, invokes print! on each of its arguments, which can be of arbitrary type and are separated by a ,:
macro_rules! printall {
( $( $arg:expr ), * ) => ( {$( print!("{} / ", $arg) ); *} );
}
When called with printall!("hello", 42, 3.14); it will print out:
hello / 42 / 3.14 /
In the example, each argument (separated by commas) is substituted by a corresponding invocation of print! separated by a /. Note that on the right side we have to make a code block of the resulting print statements by enclosing it in { }.
Read now
Unlock full access