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 →
What to know first
- 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.
- identifierAn identifier is a name or value that denotes or distinguishes an entity or program element within a system.
- patternA pattern is a specification of criteria used to match values or text.
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 typekind of · A recursive type is a type whose definition refers to itself, directly or indirectly, and can represent recursively structured values.
- treekind of · A tree is a connected, acyclic structure of nodes organized through parent-child relationships, typically around a single root.
- fragment specifierincludes · 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.
Questions about token tree
What is token tree?
A token tree is a recursive Rust macro syntax structure consisting of tokens and nested delimited sequences of tokens.
Which Manning books explain token tree?
Write Powerful Rust Macros, Idiomatic Rust and Learn Rust in a Month of Lunches.
What should I know before learning token tree?
Manning books that teach token tree usually introduce domain-specific language, identifier and pattern first.
What kinds of token tree are there?
Kinds of token tree include fragment specifier.
What is token tree a kind of?
Token tree is a kind of recursive type. A recursive type is a type whose definition refers to itself, directly or indirectly, and can represent recursively structured values.
What is token tree part of?
Token tree is part of TokenStream. TokenStream is a sequence of Rust source tokens used as input or output during procedural macro expansion and code generation.
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.


