[][src]Struct syn::Ident

pub struct Ident { /* fields omitted */ }

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.

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]

impl<T: ?Sized> PartialEq<T> for Ident where
    T: AsRef<str>, 
[src]

impl Ord for Ident[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]

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]

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> From<T> for T[src]

impl<T> ToString for T where
    T: Display + ?Sized
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> Any for T where
    T: 'static + ?Sized
[src]