Macro synstructure::quote []

macro_rules! quote {
    ( $ ( $ tt : tt ) * ) => { ... };
}

The whole point.

Performs variable interpolation against the input and produces it as TokenStream. For returning tokens to the compiler in a procedural macro, use into() to build a TokenStream.

Interpolation

Variable interpolation is done with #var (similar to $var in macro_rules! macros). This grabs the var variable that is currently in scope and inserts it in that location in the output tokens. Any type implementing the ToTokens trait can be interpolated. This includes most Rust primitive types as well as most of the syntax tree types from the Syn crate.

Repetition is done using #(...)* or #(...),* again similar to macro_rules!. This iterates through the elements of any variable interpolated within the repetition and inserts a copy of the repetition body for each one. The variables in an interpolation may be anything that implements IntoIterator, including Vec or a pre-existing iterator.

Hygiene

Any interpolated tokens preserve the Span information provided by their ToTokens implementation. Tokens that originate within the quote! invocation are spanned with Span::call_site().

A different span can be provided through the quote_spanned! macro.

Example

extern crate proc_macro;

#[macro_use]
extern crate quote;

use proc_macro::TokenStream;

#[proc_macro_derive(HeapSize)]
pub fn derive_heap_size(input: TokenStream) -> TokenStream {
    // Parse the input and figure out what implementation to generate...
    let name = /* ... */;
    let expr = /* ... */;

    let expanded = quote! {
        // The generated impl.
        impl ::heapsize::HeapSize for #name {
            fn heap_size_of_children(&self) -> usize {
                #expr
            }
        }
    };

    // Hand the output tokens back to the compiler.
    expanded.into()
}