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"));
Traits
-
IdentFragment
Specialized formatting trait used by
format_ident!. -
ToTokens
Types that can be interpolated inside a
quote!invocation. - TokenStreamExt TokenStream extension trait with methods for appending tokens.
Macros
-
format_ident
Formatting macro for constructing
Idents. - quote The whole point.
-
quote_spanned
Same as
quote!, but applies a given span to all tokens originating within the macro invocation.