diff options
author | Nika Layzell <nika@thelayzells.com> | 2019-08-16 12:23:29 -0700 |
---|---|---|
committer | Nika Layzell <nika@thelayzells.com> | 2019-08-16 12:23:29 -0700 |
commit | 35ec18343cc021613efe3054d66f279bf51230f3 (patch) | |
tree | 9a78897a64f9af22163daf2e8ee86ea4f3b3109c | |
parent | c6a5958a8738670422ed9ea59e0e61f28bcd16c2 (diff) | |
download | platform_external_rust_crates_quote-35ec18343cc021613efe3054d66f279bf51230f3.tar.gz platform_external_rust_crates_quote-35ec18343cc021613efe3054d66f279bf51230f3.tar.bz2 platform_external_rust_crates_quote-35ec18343cc021613efe3054d66f279bf51230f3.zip |
Encourage format_ident over Ident::new()
-rw-r--r-- | README.md | 16 | ||||
-rw-r--r-- | src/lib.rs | 28 |
2 files changed, 24 insertions, 20 deletions
@@ -148,22 +148,24 @@ 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 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. ```rust -let varname = format_ident!("_{}", ident); +let concatenated = format!("_{}", ident); +let varname = syn::Ident::new(&concatenated, ident.span()); quote! { let mut #varname = 0; } @@ -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; /// } |