macro

Forwarding captured fragments

Choose token trees when a downstream macro needs to distinguish literal tokens. Parsed expression fragments instead retain their syntax category and cannot generally be rematched as individual literal tokens. Expected stdout (not executed): 'stop go\n' Common mistake: Assuming an expr fragment can be inspected as a literal token by the next macro.

Formal Syntax & Structure

rust syntaxrust
$t:tt => next!($t)

Working Example

Example Snippetrust
macro_rules! classify { (red) => { "stop" }; ($other:ident) => { "go" }; }macro_rules! forward { ($t:tt) => { classify!($t) }; }fn main() { println!("{} {}", forward!(red), forward!(green)); }
Rust Reference