diff options
author | David Tolnay <dtolnay@gmail.com> | 2019-08-16 12:49:53 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-08-16 12:49:53 -0700 |
commit | 40b985223063562beaad0c86a07052a004ed8fb6 (patch) | |
tree | 9a78897a64f9af22163daf2e8ee86ea4f3b3109c | |
parent | 78f551dd7ce6b58c4b7e783bdfc8d1221c91b9c0 (diff) | |
parent | 35ec18343cc021613efe3054d66f279bf51230f3 (diff) | |
download | quote-40b985223063562beaad0c86a07052a004ed8fb6.tar.gz |
Merge pull request #128 from mystor/format_ident_examples
Encourage format_ident! over Ident::new in docs
-rw-r--r-- | README.md | 16 | ||||
-rw-r--r-- | src/lib.rs | 28 |
2 files changed, 29 insertions, 15 deletions
@@ -148,8 +148,20 @@ quote! { } ``` -The solution is to perform token-level manipulations using the APIs provided by -Syn and proc-macro2. +The solution is to build a new identifier token with the correct value. As this +is such a common case, the `format_ident!` macro provides a convenient utility +for doing so correctly. + +```rust +let varname = format_ident!("_{}", ident); +quote! { + let mut #varname = 0; +} +``` + +Alternatively, the APIs provided by Syn and proc-macro2 can be used to directly +build the identifier. This is roughly equivalent to the above, but will not +handle `ident` being a raw identifier. ```rust let concatenated = format!("_{}", ident); @@ -265,33 +265,35 @@ pub mod spanned; /// # ; /// ``` /// -/// The solution is to perform token-level manipulations using the APIs provided -/// by Syn and proc-macro2. +/// The solution is to build a new identifier token with the correct value. As +/// this is such a common case, the [`format_ident!`] macro provides a +/// convenient utility for doing so correctly. /// /// ``` -/// # use proc_macro2::{self as syn, Span}; -/// # use quote::quote; +/// # use proc_macro2::{Ident, Span}; +/// # use quote::{format_ident, quote}; /// # -/// # let ident = syn::Ident::new("i", Span::call_site()); +/// # let ident = Ident::new("i", Span::call_site()); /// # -/// let concatenated = format!("_{}", ident); -/// let varname = syn::Ident::new(&concatenated, ident.span()); +/// let varname = format_ident!("_{}", ident); /// quote! { /// let mut #varname = 0; /// } /// # ; /// ``` /// -/// For identifier concatenation specifically, since this is such a common case, -/// the [`format_ident!`] macro provides a more concise equivalent of the above. +/// Alternatively, the APIs provided by Syn and proc-macro2 can be used to +/// directly build the identifier. This is roughly equivalent to the above, but +/// will not handle `ident` being a raw identifier. /// /// ``` -/// # use proc_macro2::{Ident, Span}; -/// # use quote::{format_ident, quote}; +/// # use proc_macro2::{self as syn, Span}; +/// # use quote::quote; /// # -/// # let ident = Ident::new("i", Span::call_site()); +/// # let ident = syn::Ident::new("i", Span::call_site()); /// # -/// let varname = format_ident!("_{}", ident); +/// let concatenated = format!("_{}", ident); +/// let varname = syn::Ident::new(&concatenated, ident.span()); /// quote! { /// let mut #varname = 0; /// } |