A token tree is a token or a delimited group of tokens that a Rust macro can match. The `tt` fragment lets a macro accept flexible input without describing its precise expression or type. section 22.3
Token-tree patterns become especially useful with repetition:
- `$arg:tt` matches one token-tree argument. section 22.3
- `$($arg:tt)*` matches zero, one, or many token trees. section 22.3
- `+` requires one or more repetitions, while `?` makes a fragment optional. section 22.3
Forwarding arbitrary arguments
`write!` captures a destination expression and any number of token trees, then forwards those token trees to `format_args!`. This pattern explains why formatting macros accept varied arguments.
macro_rules! todo {
() => (panic!("not yet implemented"));
($($arg:tt)+) => (panic!("not yet implemented: {}", $crate::format_args!($($arg)+)));
}
`todo!` contrasts an empty pattern with a repeated token-tree pattern, showing how a macro can support both no arguments and formatted arguments. section 22.3


