Struct Ident

Source
pub struct Ident {
    pub span: Span,
    /* private fields */
}
Expand description

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::Lifetime instead.

An identifier constructed with Ident::new is permitted to be a Rust keyword, though parsing one through its Synom implementation rejects Rust keywords.

§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) { /* ... */ }

Fields§

§span: Span

Implementations§

Source§

impl Ident

Source

pub fn new(s: &str, span: Span) -> Self

Creates an ident with the given string representation.

§Panics

Panics if the input string is neither a keyword nor a legal variable name.

Trait Implementations§

Source§

impl AsRef<str> for Ident

Source§

fn as_ref(&self) -> &str

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl Clone for Ident

Source§

fn clone(&self) -> Ident

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Ident

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Display for Ident

Source§

fn fmt(&self, formatter: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'a> From<&'a str> for Ident

Source§

fn from(s: &str) -> Self

Converts to this type from the input type.
Source§

impl From<CapSelf> for Ident

Source§

fn from(tok: CapSelf) -> Self

Converts to this type from the input type.
Source§

impl<'a> From<Cow<'a, str>> for Ident

Source§

fn from(s: Cow<'a, str>) -> Self

Converts to this type from the input type.
Source§

impl From<Crate> for Ident

Source§

fn from(tok: Crate) -> Self

Converts to this type from the input type.
Source§

impl From<Ident> for Meta

Source§

fn from(e: Ident) -> Meta

Converts to this type from the input type.
Source§

impl From<Ident> for TypeParam

Source§

fn from(ident: Ident) -> Self

Converts to this type from the input type.
Source§

impl From<Self_> for Ident

Source§

fn from(tok: Self_) -> Self

Converts to this type from the input type.
Source§

impl From<String> for Ident

Source§

fn from(s: String) -> Self

Converts to this type from the input type.
Source§

impl From<Super> for Ident

Source§

fn from(tok: Super) -> Self

Converts to this type from the input type.
Source§

impl Hash for Ident

Source§

fn hash<H: Hasher>(&self, h: &mut H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl Ord for Ident

Source§

fn cmp(&self, other: &Ident) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl<T> PartialEq<T> for Ident
where T: AsRef<str> + ?Sized,

Source§

fn eq(&self, other: &T) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialOrd for Ident

Source§

fn partial_cmp(&self, other: &Ident) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl Synom for Ident

Source§

fn parse(input: Cursor<'_>) -> PResult<'_, Self>

Source§

fn description() -> Option<&'static str>

Source§

impl ToTokens for Ident

Source§

fn to_tokens(&self, tokens: &mut Tokens)

Write self to the given Tokens. Read more
Source§

fn into_tokens(self) -> Tokens
where Self: Sized,

Convert self directly into a Tokens object. Read more
Source§

impl Copy for Ident

Source§

impl Eq for Ident

Auto Trait Implementations§

§

impl Freeze for Ident

§

impl RefUnwindSafe for Ident

§

impl !Send for Ident

§

impl !Sync for Ident

§

impl Unpin for Ident

§

impl UnwindSafe for Ident

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Spanned for T
where T: ToTokens,

Source§

fn span(&self) -> Span

Returns a Span covering the complete contents of this syntax tree node, or Span::call_site() if this node is empty.
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.