Token tree
A token tree is a recursive Rust macro syntax structure consisting of tokens and nested delimited sequences of tokens.
Write Powerful Rust Macros
A Rust token tree is a syntactic token or delimited sequence of tokens that can be captured as a broadly permissive macro fragment.
- How it is used
- Captures and forwards broadly shaped macro input when narrower declarative fragments cannot express it.
- How deep it goes
- Defines TokenTrees within TokenStreams and demonstrates matcher capture and direct procedural-macro inspection, while deferring complete syntax parsing.
- Examples
- Uses a `lazy_static!` trailing matcher and a derive macro that extracts a struct or enum name.
- What you can do after
- Apply it via `macro_rules!` matchers or procedural TokenStream iteration when macro input must be captured or inspected.
Idiomatic Rust
A token tree is a recursive syntactic structure containing tokens, sequences of tokens, or nested token trees for macro matching.
- How it is used
- Supports variadic macro matching and forwards arbitrary argument token sequences to another macro.
- How deep it goes
- Defines recursive token trees, repetition quantifiers, and transcription, but does not develop broader macro parsing internals.
- Examples
- Uses special_println!, println!, format!, format_args!, and var_print! to demonstrate capture, forwarding, and repeated identifiers.
- What you can do after
- Build macro_rules! matchers that capture repeated token trees and replay them in an expansion.
Learn Rust in a Month of Lunches
A token tree is a syntactic token or delimited group of tokens that can be matched and repeated by a Rust macro.
- How it is used
- Explains how declarative macros accept and repeat flexible input.
- How deep it goes
- Defines `tt` and demonstrates repetition, but stops short of compiler internals behind built-in macros.
- Examples
- Uses `write!`, `format_args!`, and `todo!` to show single, repeated, empty, and formatted token-tree inputs.
- What you can do after
- Evaluate macro definitions to infer whether they accept one, many, optional, or formatted inputs.
Read how Learn Rust in a Month of Lunches explains token tree →
Related concepts
- TokenStreampart of · TokenStream is a sequence of Rust source tokens used as input or output during procedural macro expansion and code generation.
- recursive data structurekind of · A recursive data structure is a structure whose definition directly or indirectly refers to another instance of the same structure.
- fragment specifieruses · A fragment specifier is a declarative-macro matcher annotation that specifies the syntactic category of tokens a metavariable can match.
- token-tree fragment specifierused by · A token-tree fragment specifier is a Rust declarative-macro matcher specifier that matches a token tree, including delimited groups.
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.


