liveBook · Manning

Metavariable

As explained in Write Powerful Rust Macros

A metavariable is a declarative-macro binding whose fragment specifier determines the syntactic category of captured input.

A metavariable is a declarative-macro binding whose fragment specifier determines the syntactic category of captured Rust input. The dollar sign marks the binding; without it, the name is treated as a literal. section 2.1.4

Matching one expression

The matcher {$x:expr} captures one Rust expression as x. The expansion can then push $x into a newly created vector. section 2.1.4

Choose a fragment specifier that constrains the intended input:

  • Use expr for expressions, ident for identifiers or keywords, block for blocks, and literal or type for those syntactic categories. section 2.1.4
  • Use tt (TokenTree) when broader matching is necessary, but test carefully because it accepts almost anything and makes matchers less fine-grained. section 2.1.4

How Write Powerful Rust Macros covers it

How deep it goes
Defines fragment matching, demonstrates expression capture, and compares precise specifiers with broad tt matching.
Examples
Uses expression capture to push a matched value into a vector and contrasts precise matching with tt.
What you can do after
Apply it via Rust declarative macros to capture expressions and choose appropriately constrained fragment specifiers.
How it connects
Connects declarative macros with macro matchers, expressions, identifiers, literals, types, blocks, and token trees.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Write Powerful Rust Macros. The section links point to the parts of the book it draws on.