Handling optional trailing commas in macro_rules!

Here’s a nice little trick I discovered whilst dissecting the router! macro for iron-middlefiddle. You can optionally allow trailing commas in a macro with the following:


If you’re unfamiliar with the macro syntax in Rust then here’s how it breaks down:

1// Start a repetition:
3// It's a repetition of commas:
5// End the repetition:
7// Allow zero or more of these repetitions:

And here’s how it looks in action:

1macro_rules! router {
2 (
3 // Start a repetition:
4 $(
5 // The pattern of each repetition:
6 $route_id:ident: $method:ident $glob:expr => $handler:expr
7 // End the repetition:
8 )
9 // Each repetition will be separated by a comma:
10 ,
11 // Allow one or more of these repetitions:
12 +
13 // Allow zero or more commas after the last repetition:
14 $(,)*
15 ) => { ... };

As you have probably deduced, it’s not an ideal solution because it not only makes zero or one trailing commas valid; but also two, three, four or sixteen trailing commas valid. Overall though, I think this is a nicer way of handling things than the two other options:

  1. Explicitly stating you expect or do not expect a trailing comma, resulting in a compile error if the user respectively does not or does leave a trailing comma. This option gets a bonus thumbs-down because such a treatment of trailing commas is not in keeping with the rest of the Rust language.

  2. Adding repetitious code in the macro to explicitly handle zero or one (and only one) trailing commas.