macro

Optional trailing separators

Separate list entries with commas and accept one trailing comma using an optional group. A trailing comma makes editing multiline lists convenient. Expected stdout (not executed): '5 10\n' Common mistake: Putting a separator on a ? repetition is invalid; put the comma inside the optional group.

Formal Syntax & Structure

rust syntaxrust
$( $n:expr ),+ $(,)?

Working Example

Example Snippetrust
macro_rules! sum { ($($n:expr),+ $(,)?) => { 0 $(+ $n)+ }; }fn main() { println!("{} {}", sum!(2, 3,), sum!(10)); }
Rust Reference