Crate quote [] [src]

Quasi-quoting without a Syntex dependency, intended for use with Macros 1.1.

[dependencies]
quote = "0.3"
#[macro_use]
extern crate quote;

Interpolation is done with #var:

let tokens = quote! {
    struct SerializeWith #generics #where_clause {
        value: &'a #field_ty,
        phantom: ::std::marker::PhantomData<#item_ty>,
    }

    impl #generics serde::Serialize for SerializeWith #generics #where_clause {
        fn serialize<S>(&self, s: &mut S) -> Result<(), S::Error>
            where S: serde::Serializer
        {
            #path(self.value, s)
        }
    }

    SerializeWith {
        value: #value,
        phantom: ::std::marker::PhantomData::<#item_ty>,
    }
};

Repetition is done using #(...)* or #(...),* very similar to macro_rules!:

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

The return type of quote! is quote::Tokens. Tokens can be interpolated into other quotes:

let t = quote! { /* ... */ };
return quote! { /* ... */ #t /* ... */ };

Call to_string() or as_str() on a Tokens to get a String or &str of Rust code.

The quote! macro relies on deep recursion so some large invocations may fail with "recursion limit reached" when you compile. If it fails, bump up the recursion limit by adding #![recursion_limit = "128"] to your crate. An even higher limit may be necessary for especially large invocations.

Macros

quote

The whole point.

Structs

ByteStr

Wrap a &str so it interpolates as a byte-string: b"abc".

Hex

Wrap an integer so it interpolates as a hexadecimal.

Ident

An identifier that should be interpolated without quotes.

Tokens

Tokens produced by a quote!(...) invocation.

Traits

ToTokens

Types that can be interpolated inside a quote!(...) invocation.