Macro repetition
Macro repetition is declarative syntax for matching or expanding a token sequence zero or more, one or more, or optionally repeated times.
Learn Rust in a Month of Lunches
Macro repetition is declarative macro syntax for matching or expanding zero or more or one or more repeated token sequences with optional separators.
- How it is used
- Uses repetition to match variable-length macro input and reproduce matched tokens in expansions.
- How deep it goes
- Works through separators and all three repetition operators, including empty input and optional clauses, without descending into compiler internals.
- Examples
- Shows a printing macro accepting empty and comma-separated calls, a function-generating macro, and optional syntax in `matches!`.
- What you can do after
- Build it from scratch: write macro rules for repeated inputs and optional syntax, then adjust separators to match intended calls.
Read how Learn Rust in a Month of Lunches explains macro repetition →
Write Powerful Rust Macros
A macro repetition operator specifies whether a repeated macro pattern occurs zero or more, one or more, or zero or one times.
- How it is used
- Uses repetition operators to accept repeated expressions and emit corresponding repeated statements in declarative macros.
- How deep it goes
- Defines *, +, and ?, demonstrates matcher-transcriber depth, and notes an unresolved operator-mismatch bug.
- Examples
- Uses a repeated-expression macro that pushes each capture into a vector and rejects mixed input types.
- What you can do after
- Apply it via Rust declarative macros to accept repeated expressions and generate repeated operations.
Read how Write Powerful Rust Macros explains macro repetition →
Related concepts
- macro matcherconfigures · A macro matcher is the pattern portion of a declarative macro rule that determines which invocation tokens the rule accepts.
- quantifiercontrasts with · A regular-expression quantifier is syntax that specifies how many times a preceding pattern element may or must match.
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.

