[−][src]Struct syn::Ident
A word of Rust code, which may be a keyword or legal variable name.
An identifier consists of at least one Unicode code point, the first of which has the XID_Start property and the rest of which have the XID_Continue property. An underscore may be used as the first character as long as it is not the only character.
- The empty string is not an identifier. Use
Option<Ident>. - An underscore by itself is not an identifier. Use
Token![_]instead. - A lifetime is not an identifier. Use
syn::Lifetimeinstead.
An identifier constructed with Ident::new is permitted to be a Rust
keyword, though parsing one through its Synom implementation rejects
Rust keywords. Use call!(Ident::parse_any) when parsing to match the
behaviour of Ident::new.
Examples
A new ident can be created from a string using the Ident::from function.
Idents produced by Ident::from are set to resolve at the procedural macro
def site by default. A different span can be provided explicitly by using
Ident::new.
extern crate syn; extern crate proc_macro2; use syn::Ident; use proc_macro2::Span; fn main() { let def_ident = Ident::from("definitely"); let call_ident = Ident::new("calligraphy", Span::call_site()); println!("{} {}", def_ident, call_ident); }
An ident can be interpolated into a token stream using the quote! macro.
#[macro_use] extern crate quote; extern crate syn; use syn::Ident; fn main() { let ident = Ident::from("demo"); // Create a variable binding whose name is this ident. let expanded = quote! { let #ident = 10; }; // Create a variable binding with a slightly different name. let temp_ident = Ident::from(format!("new_{}", ident)); let expanded = quote! { let #temp_ident = 10; }; }
A string representation of the ident is available through the as_ref() and
to_string() methods.
// Examine the ident as a &str. let ident_str = ident.as_ref(); if ident_str.len() > 60 { println!("Very long identifier: {}", ident_str) } // Create a String from the ident. let ident_string = ident.to_string(); give_away(ident_string); fn give_away(s: String) { /* ... */ }
Methods
impl Ident[src]
pub fn parse_any(input: Cursor) -> PResult<Self>[src]
Parses any identifier
This is useful when parsing a DSL which allows Rust keywords as identifiers.
impl Ident[src]
pub fn new(s: &str, span: Span) -> Self[src]
Creates an ident with the given string representation.
Panics
Panics if the input string is neither a keyword nor a legal variable name.
pub fn span(&self) -> Span[src]
pub fn set_span(&mut self, span: Span)[src]
Trait Implementations
impl Synom for Ident[src]
impl Eq for Ident[src]
impl Clone for Ident[src]
impl AsRef<str> for Ident[src]
impl PartialOrd<Ident> for Ident[src]
fn partial_cmp(&self, other: &Ident) -> Option<Ordering>[src]
#[must_use]
fn lt(&self, other: &Rhs) -> bool1.0.0[src]
#[must_use]
fn le(&self, other: &Rhs) -> bool1.0.0[src]
#[must_use]
fn gt(&self, other: &Rhs) -> bool1.0.0[src]
#[must_use]
fn ge(&self, other: &Rhs) -> bool1.0.0[src]
impl<T: ?Sized> PartialEq<T> for Ident where
T: AsRef<str>, [src]
T: AsRef<str>,
impl Ord for Ident[src]
fn cmp(&self, other: &Ident) -> Ordering[src]
fn max(self, other: Self) -> Self1.21.0[src]
fn min(self, other: Self) -> Self1.21.0[src]
fn clamp(self, min: Self, max: Self) -> Self[src]
impl From<Ident> for Meta[src]
impl From<Ident> for TypeParam[src]
impl<'a> From<&'a str> for Ident[src]
impl From<Self_> for Ident[src]
impl From<CapSelf> for Ident[src]
impl From<Super> for Ident[src]
impl From<Crate> for Ident[src]
impl<'a> From<Cow<'a, str>> for Ident[src]
impl From<String> for Ident[src]
impl Copy for Ident[src]
impl Hash for Ident[src]
fn hash<H: Hasher>(&self, h: &mut H)[src]
fn hash_slice<H>(data: &[Self], state: &mut H) where
H: Hasher, 1.3.0[src]
H: Hasher,
impl Debug for Ident[src]
impl Display for Ident[src]
impl ToTokens for Ident[src]
Auto Trait Implementations
impl !Send for Ident
impl Unpin for Ident
impl !Sync for Ident
impl UnwindSafe for Ident
impl !RefUnwindSafe for Ident
Blanket Implementations
impl<T> Spanned for T where
T: ToTokens, [src]
T: ToTokens,
impl<T> ToOwned for T where
T: Clone, [src]
T: Clone,
type Owned = T
The resulting type after obtaining ownership.
fn to_owned(&self) -> T[src]
fn clone_into(&self, target: &mut T)[src]
impl<T, U> Into<U> for T where
U: From<T>, [src]
U: From<T>,
impl<T> From<T> for T[src]
impl<T> ToString for T where
T: Display + ?Sized, [src]
T: Display + ?Sized,
impl<T, U> TryFrom<U> for T where
U: Into<T>, [src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>, [src]
U: TryFrom<T>,
type Error = <U as TryFrom<T>>::Error
The type returned in the event of a conversion error.
fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>[src]
impl<T> BorrowMut<T> for T where
T: ?Sized, [src]
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T[src]
impl<T> Borrow<T> for T where
T: ?Sized, [src]
T: ?Sized,
impl<T> Any for T where
T: 'static + ?Sized, [src]
T: 'static + ?Sized,