macro

Repeat captures with star and plus

Use * for zero or more entries and + when at least one is required. The expansion repeats the corresponding captured values, so the same rule handles different list lengths. Expected stdout (not executed): '0 16 4\n' Common mistake: Using + when an empty list should be valid. The first macro accepts zero entries.

Formal Syntax & Structure

rust syntaxrust
$( $x:expr ),* / $( $x:expr ),+

Working Example

Example Snippetrust
macro_rules! total { ($($n:expr),*) => { 0 $(+ $n)* }; }macro_rules! first { ($head:expr $(, $rest:expr)*) => { $head }; }fn main() { println!("{} {} {}", total!(), total!(2, 5, 9), first!(4, 7)); }
Rust Reference