liveBook · Manning

Token-tree fragment specifier

As explained in Learn Rust in a Month of Lunches

A token-tree fragment specifier matches a token tree, allowing a broad range of syntactic input in a macro pattern.

A token-tree fragment specifier, written `$input:tt`, lets a macro capture broad token input without requiring it to be an expression, statement, type, or another narrower fragment kind. The captured token can then be reused in the macro output. section 22.2

Capture and print arbitrary tokens

A macro using `$input:tt` can capture unusual identifiers or number-like tokens and print them with `stringify!`. Repetition extends the pattern to comma-separated input, including an empty list when `*` is used.

How Learn Rust in a Month of Lunches covers it

How deep it goes
Defines `tt` among fragment specifiers and demonstrates single and repeated captures, but does not detail token-tree parsing internals.
Examples
Demonstrates strange identifiers, numeric-looking tokens, comma-separated lists, and generated functions.
What you can do after
Builds from scratch macros that capture and reuse one token tree or repeated token trees.
How it connects
Connects token trees with `macro_rules!`, `stringify!`, repetition, and narrower fragments such as `expr` and `stmt`.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn Rust in a Month of Lunches. The section links point to the parts of the book it draws on.