Start with the idea
macro_rules! consumes token trees with balanced delimiters. Fragment specifiers such as expr and ty let the parser validate shapes before expansion, producing safer patterns than string substitution.
Rust tokenizes macro input into structured pieces. Identifiers, punctuation, and literals are tokens; balanced parentheses, brackets, and braces form delimited groups that can contain more tokens. A macro does not receive raw characters to split by hand. In a function-like invocation such as show!({a, b}), the name show selects the macro, while the input stream inside the invocation contains a brace group with a, a comma, and b. Keeping that boundary clear prevents confusion when reading a procedural macro parser.
A macro_rules!
Keep learning, for free
The rest of this lesson is waiting for you.
Create a free account to read the complete explanation, see the infographic, explore the code, and try the exercises. All 742 lessons and algorithm steps are included.
No payment details. Your account opens the complete Academy.