Macro quote::quote [] [src]

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

The whole point.

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


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. The variable must implement the ToTokens trait.

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.

  • #(#var)* — no separators
  • #(#var),* — the character before the asterisk is used as a separator
  • #( struct #var; )* — the repetition can contain other tokens
  • #( #k => println!("{}", #v), )* — even multiple interpolations


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.


extern crate proc_macro;

extern crate quote;

use proc_macro::TokenStream;

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 {

    // Hand the output tokens back to the compiler.