Module quote

Quasi-quoting for generating Rust source code as tokens.


quote provides the quote! macro, which turns Rust syntax into a proc_macro2::TokenStream. It is one of the three foundational crates for writing procedural macros, alongside syn for parsing and proc_macro2 for token representation.

The quote! macro supports interpolation of variables with #var and repetition with #( ... )*, mirroring the patterns of macro_rules!.

Example

Generating a token stream with interpolation:

use quote::quote;
use proc_macro2::Ident;

let name = Ident::new("Greeter", proc_macro2::Span::call_site());
let field_name = Ident::new("message", proc_macro2::Span::call_site());

let tokens = quote! {
    struct #name {
        #field_name: String,
    }
};

let code = tokens.to_string();
assert!(code.contains("struct Greeter"));
assert!(code.contains("message : String"));

Macros