Declarative macro
macro_rules! is Rust syntax for defining declarative macros by matching input token patterns and expanding matching rules into generated source code.
Write Powerful Rust Macros
macro_rules! is Rust's declaration syntax for defining declarative macros from matcher and transcriber rules.
- How it is used
- Eliminates boilerplate and supplies defaults, varargs, DSLs, and function-composition syntax through reusable code-generation rules.
- How deep it goes
- Explains rule syntax, captures, repetition, recursion, visibility, hygiene, and failure cases, while deferring lower-level token manipulation to procedural macros.
- Examples
- Demonstrates my_vec, greeting defaults, generated newtype getters, recursive composition, currying closures, and generated AWS error conversions.
- What you can do after
- Build it from scratch by defining matcher–transcriber rules, capturing fragments, repeating inputs, and recursively delegating default cases.
Read how Write Powerful Rust Macros explains declarative macro →
Learn Rust in a Month of Lunches
macro_rules! is Rust syntax for defining declarative macros by matching token sequences and expanding them into source code.
- How it is used
- Defines reusable source transformations by matching macro input and expanding it before ordinary compilation.
- How deep it goes
- Explains exact matching, fragment capture, repetition, diagnostics, and generated functions, but stops short of deeper macro internals.
- Examples
- Uses give_six!, six_or_print!, expression and statement captures, token-tree printing, repetitions, and generated functions.
- What you can do after
- Build declarative macros that match literals or fragments, repeat inputs, substitute captures, and interpret compiler feedback.
Read how Learn Rust in a Month of Lunches explains declarative macro →
Idiomatic Rust
Rust's macro_rules! syntax defines declarative macros by associating input token patterns with expansion templates.
- How it is used
- Generates code at compile time to handle matching syntax and reduce verbose, repetitive implementations.
- How deep it goes
- Explains matching rules, fragment captures, and unmatched-input errors, then defers more complex cases to procedural macros.
- Examples
- Demonstrates no-op and fragment-matching macros, then generates dog-breed structs and trait implementations.
- What you can do after
- Build it from scratch by mapping an identifier to repeated struct, constructor, and trait code.
Related concepts
- macrokind of · A macro is a named construct that expands or transforms source code, commands, or templates before or during execution.
- macro hygieneconfigures · Macro hygiene is a property of macro expansion that prevents introduced or referenced identifiers from unintentionally conflicting with surrounding code.
- macro transcriberhas part · A macro transcriber is the output-template portion of a declarative macro definition that specifies generated syntax for a matched invocation.
- domain-specific languageuses · A domain-specific language is a specialized language, notation, or terminology tailored to express concepts and tasks within a particular subject domain.
- macro matcherhas part · A macro matcher is the pattern portion of a declarative macro rule that determines which invocation tokens the rule accepts.
These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.


