liveBook · Manning

Identifier fragment specifier

As explained in Learn Rust in a Month of Lunches

An identifier fragment specifier matches a Rust identifier such as a variable or function name as macro input.

An identifier fragment specifier, written as $name:ident, lets a macro match an identifier such as a variable or function name. The matched name can then be reused in the generated Rust code. section 22.2

Comparing a name with an expression

A macro can accept an identifier and an expression separately: check!(x, 6) matches x as $input1:ident and 6 as $input2:expr. The same pattern also accepts my_vec as the identifier and vec![7, 8, 9] as the expression.

How Learn Rust in a Month of Lunches covers it

How deep it goes
Defines ident through focused macro examples but defers the complete fragment-specifier details.
Examples
Demonstrates identifier matching with x, my_vec, print_it, and say_its_nice.
What it assumes
Assumes basic Rust macros and distinguishes identifiers from expressions through examples.
What you can do after
Apply it via macro_rules! to capture variable or function names and reuse them in generated code.
How it connects
Connects ident with fragment specifiers, macro_rules!, expressions, token trees, and generated functions.

Also explained in

Compare how 2 books explain identifier fragment specifier →

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.