4 [![Build Status](https://api.travis-ci.org/dtolnay/quote.svg?branch=master)](https://travis-ci.org/dtolnay/quote)
5 [![Latest Version](https://img.shields.io/crates/v/quote.svg)](https://crates.io/crates/quote)
6 [![Rust Documentation](https://img.shields.io/badge/api-rustdoc-blue.svg)](https://docs.rs/quote/)
8 Quasi-quoting without a Syntex dependency, intended for use with [Macros
9 1.1](https://github.com/rust-lang/rfcs/blob/master/text/1681-macros-1.1.md).
21 ## What is quasi-quoting?
23 Quasi-quoting is a way of writing code and treating it as data, similar to
24 writing code inside of a double-quoted string literal except more friendly to
25 your text editor or IDE. It does not get in the way of syntax highlighting,
26 brace matching, indentation, or autocompletion, all of which you would lose by
27 writing code inside of double quotes.
30 [my meetup talk](https://air.mozilla.org/rust-meetup-december-2016-12-15/)
31 on the topic to learn more about the use case. Start the video at 3:00.
33 This crate is motivated by the Macros 1.1 use case, but is a general-purpose
34 Rust quasi-quoting library and is not specific to procedural macros.
38 The quote crate provides a `quote!` macro within which you can write Rust code
39 that gets packaged into a `quote::Tokens` and can be treated as data. You should
40 think of `quote::Tokens` as representing a fragment of Rust source code. Call
41 `to_string()` or `as_str()` on a Tokens to get back the fragment of source code
44 Within the `quote!` macro, interpolation is done with `#var`. Any type
45 implementing the `quote::ToTokens` trait can be interpolated. This includes most
46 Rust primitive types as well as most of the syntax tree types from
47 [`syn`](https://github.com/dtolnay/syn).
51 struct SerializeWith #generics #where_clause {
53 phantom: ::std::marker::PhantomData<#item_ty>,
56 impl #generics serde::Serialize for SerializeWith #generics #where_clause {
57 fn serialize<S>(&self, s: &mut S) -> Result<(), S::Error>
58 where S: serde::Serializer
66 phantom: ::std::marker::PhantomData::<#item_ty>,
71 Repetition is done using `#(...)*` or `#(...),*` very similar to `macro_rules!`:
73 - `#(#var)*` - no separators
74 - `#(#var),*` - the character before the asterisk is used as a separator
75 - `#( struct #var; )*` - the repetition can contain other things
76 - `#( #k => println!("{}", #v), )*` - even multiple interpolations
78 Tokens can be interpolated into other quotes:
81 let t = quote! { /* ... */ };
82 return quote! { /* ... */ #t /* ... */ };
85 The `quote!` macro relies on deep recursion so some large invocations may fail
86 with "recursion limit reached" when you compile. If it fails, bump up the
87 recursion limit by adding `#![recursion_limit = "128"]` to your crate. An even
88 higher limit may be necessary for especially large invocations. You don't need
89 this unless the compiler tells you that you need it.
93 Licensed under either of
95 * Apache License, Version 2.0 ([LICENSE-APACHE](LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0)
96 * MIT license ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT)
102 Unless you explicitly state otherwise, any contribution intentionally submitted
103 for inclusion in this crate by you, as defined in the Apache-2.0 license, shall
104 be dual licensed as above, without any additional terms or conditions.