liveBook · Manning

Declarative macro

macro_rules! is Rust syntax for defining declarative macros by matching input token patterns and expanding matching rules into generated source code.

How 3 Manning books explain it

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.

Read how Idiomatic Rust explains declarative macro →

Related concepts

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.