Declarative macro
A Rust declarative macro is defined by rules that match input token sequences and expand them into generated 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.
What to know first
- RustRust is a statically typed systems programming language designed for performance, concurrency, and memory safety without requiring garbage collection.
- domain-specific languageA domain-specific language is a language or notation tailored to expressing concepts, operations, or solutions within a particular problem or application domain.
- expressionAn expression is a syntactically formed combination of values, names, operators, or other terms that denotes or evaluates to a value or result.
- macroA macro is a named, reusable construct that expands into generated code or a sequence of commands when invoked, often during preprocessing, compilation, or application execution.
Related concepts
- macrokind of · A macro is a named, reusable construct that expands into generated code or a sequence of commands when invoked, often during preprocessing, compilation, or application execution.
- domain-specific languageuses · A domain-specific language is a language or notation tailored to expressing concepts, operations, or solutions within a particular problem or application domain.
- macro hygieneconfigures · Macro hygiene is a property of macro expansion that prevents introduced or referenced identifiers from unintentionally conflicting with surrounding code.
- macro matcherhas part · A macro matcher is the pattern portion of a declarative macro rule that determines which invocation tokens the rule accepts.
- macro transcriberhas part · A macro transcriber is the output-template portion of a declarative macro definition that specifies generated syntax for a matched invocation.
Questions about declarative macro
What is declarative macro?
A Rust declarative macro is defined by rules that match input token sequences and expand them into generated code.
Which Manning books explain declarative macro?
Write Powerful Rust Macros, Learn Rust in a Month of Lunches and Idiomatic Rust.
What should I know before learning declarative macro?
Manning books that teach declarative macro usually introduce Rust, domain-specific language, expression and macro first.
What kinds of declarative macro are there?
Kinds of declarative macro include query macro.
What is declarative macro a kind of?
Declarative macro is a kind of macro. A macro is a named, reusable construct that expands into generated code or a sequence of commands when invoked, often during preprocessing, compilation, or application execution.
What are the parts of declarative macro?
Declarative macro includes macro matcher and macro transcriber.
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.


