An expression fragment specifier, written `$input:expr`, lets a `macro_rules!` macro accept a Rust expression and reuse it under the chosen argument name. The macro matches structured input rather than only literal tokens. section 22.2
Matching expressions
A macro using `$input:expr` can print `()`, `6`, or `vec![8, 9, 7, 10]`. The compiler checks whether the generated use is valid, such as whether a value supports `Debug`. Passing `let x = 9` fails because that input is a statement, not an expression; changing the matcher to `$input:stmt` accepts it. section 22.2
macro_rules! might_print {
($input:expr) => {
println!("You gave me: {:?}", $input);
}
}
fn main() {
might_print!(());
might_print!(6);
might_print!(vec![8, 9, 7, 10]);
}
Compare several accepted expression inputs in a focused `macro_rules!` example. section 22.2
