How to Use Repetitions in Rust Macros ($(...),*)

Use $( ... )* or $( ... )+ in Rust macros to repeat token sequences zero or more times or one or more times respectively.

Use the $( ... )* syntax to repeat a sequence of tokens zero or more times, or $( ... )+ for one or more times. Place the repetition operator immediately after the closing delimiter of the token sequence.

macro_rules! repeat {
    ($($x:expr),*) => {
        $(
            println!("Item: {}", $x);
        )*
    };
}

fn main() {
    repeat!(1, 2, 3);
}

In this example, $($x:expr),* matches a comma-separated list of expressions. The * operator repeats the pattern for every item in the list. If you need a separator other than a comma, place it inside the parentheses before the operator, like $( $x:expr; )*.