Start with the idea
A declarative macro takes token trees, not values, so it can accept a variable number of arguments and shapes a function cannot. Fragment specifiers such as expr and ident say what each capture must be, and the expansion is type-checked afterwards like any other code.
A macro_rules! definition contains arms. Each arm has a pattern that matches input syntax and a transcriber that emits output syntax. The compiler tries arms in order. In maximum!, the one-expression arm ends recursion; the repeated arm captures one expression and a nonempty comma-separated rest. That is a compile-time transformation. Values are evaluated only when the expanded Rust code runs, and the expansion is type-checked in the caller's context.
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.