macro

quote!: generate tokens using interpolation

The external quote crate interpolates values that can produce tokens. Generate a declaration from a validated identifier and numeric literal, then parse the result to inspect its structure rather than relying on spacing in its display. Example: external-dependency project; see fixtures/external-token-tools/src/bin/quote.rs. This caller is not a standalone rustc program. Expected stdout (not executed): 'TOTAL\n' Common mistake: Using # interpolation outside quote syntax or assuming the generated tokens have been executed.

Formal Syntax & Structure

rust syntaxrust
quote::quote!(const #name: u32 = #value;)

Working Example

Example Snippetrust
fn main() { let name = syn::Ident::new("TOTAL", proc_macro2::Span::call_site()); let value = 42u32; let tokens = quote::quote!(const #name: u32 = #value;); let item: syn::ItemConst = syn::parse2(tokens).unwrap(); println!("{}", item.ident); }
Rust Reference