Careful, this makes the macro usable only in statement position (so no Some(foo) => test!(foo), branch, for instance).
To avoid that, have the macro emit braces ≠ the braces used to define the macro rules themselves. I personally thus use parenthesis to define the macro rules, since it's more symmetric (with the input of the macro), and makes the lack of surrounding braces more obvious:
macro_rules! test {(
$foo:ident $(,)?
) => (
{
let foo = $foo;
println!("{foo}");
}
)}
A fair point. I was aware of the distinction but I figured it wasn't worth going into, particularly since the OP and @Cerber-Ursi 's version had a trailing semicolon which currently produces a warning that links to #79813. Maybe you are right that it is worth mentioning here though.
That's a neat idea I hadn't heard of before. I might adopt that myself.