Repeat

Macros can use + in the argument list to indicate that an argument may
repeat at least once, or *, to indicate that the argument may repeat zero or
more times.

In the following example, surrounding the matcher with $(...),+ will
match one or more expression, separated by commas.
Also note that the semicolon is optional on the last case.

  1. // `min!` will calculate the minimum of any number of arguments.
  2. macro_rules! find_min {
  3. // Base case:
  4. ($x:expr) => ($x);
  5. // `$x` followed by at least one `$y,`
  6. ($x:expr, $($y:expr),+) => (
  7. // Call `find_min!` on the tail `$y`
  8. std::cmp::min($x, find_min!($($y),+))
  9. )
  10. }
  11. fn main() {
  12. println!("{}", find_min!(1u32));
  13. println!("{}", find_min!(1u32 + 2 , 2u32));
  14. println!("{}", find_min!(5u32, 2u32 * 3, 4u32));
  15. }