Skip to content

Commit 8b71dac

Browse files
committed
Copy in the Ident documentation from syn 0.13
1 parent 82ba02d commit 8b71dac

File tree

1 file changed

+77
-1
lines changed

1 file changed

+77
-1
lines changed

src/lib.rs

Lines changed: 77 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -619,7 +619,78 @@ impl fmt::Debug for Punct {
619619
}
620620
}
621621

622-
/// An identifier (`ident`).
622+
/// A word of Rust code, which may be a keyword or legal variable name.
623+
///
624+
/// An identifier consists of at least one Unicode code point, the first of
625+
/// which has the XID_Start property and the rest of which have the XID_Continue
626+
/// property.
627+
///
628+
/// - The empty string is not an identifier. Use `Option<Ident>`.
629+
/// - A lifetime is not an identifier. Use `syn::Lifetime` instead.
630+
///
631+
/// An identifier constructed with `Ident::new` is permitted to be a Rust
632+
/// keyword, though parsing one through its [`Synom`] implementation rejects
633+
/// Rust keywords. Use `call!(Ident::parse_any)` when parsing to match the
634+
/// behaviour of `Ident::new`.
635+
///
636+
/// [`Synom`]: https://docs.rs/syn/0.14/syn/synom/trait.Synom.html
637+
///
638+
/// # Examples
639+
///
640+
/// A new ident can be created from a string using the `Ident::new` function.
641+
/// A span must be provided explicitly which governs the name resolution
642+
/// behavior of the resulting identifier.
643+
///
644+
/// ```rust
645+
/// extern crate proc_macro2;
646+
///
647+
/// use proc_macro2::{Ident, Span};
648+
///
649+
/// fn main() {
650+
/// let call_ident = Ident::new("calligraphy", Span::call_site());
651+
///
652+
/// println!("{}", call_ident);
653+
/// }
654+
/// ```
655+
///
656+
/// An ident can be interpolated into a token stream using the `quote!` macro.
657+
///
658+
/// ```rust
659+
/// #[macro_use]
660+
/// extern crate quote;
661+
///
662+
/// extern crate proc_macro2;
663+
///
664+
/// use proc_macro2::{Ident, Span};
665+
///
666+
/// fn main() {
667+
/// let ident = Ident::new("demo", Span::call_site());
668+
///
669+
/// // Create a variable binding whose name is this ident.
670+
/// let expanded = quote! { let #ident = 10; };
671+
///
672+
/// // Create a variable binding with a slightly different name.
673+
/// let temp_ident = Ident::new(&format!("new_{}", ident), Span::call_site());
674+
/// let expanded = quote! { let #temp_ident = 10; };
675+
/// }
676+
/// ```
677+
///
678+
/// A string representation of the ident is available through the `to_string()`
679+
/// method.
680+
///
681+
/// ```rust
682+
/// # extern crate proc_macro2;
683+
/// #
684+
/// # use proc_macro2::{Ident, Span};
685+
/// #
686+
/// # let ident = Ident::new("another_identifier", Span::call_site());
687+
/// #
688+
/// // Examine the ident as a string.
689+
/// let ident_string = ident.to_string();
690+
/// if ident_string.len() > 60 {
691+
/// println!("Very long identifier: {}", ident_string)
692+
/// }
693+
/// ```
623694
#[derive(Clone)]
624695
pub struct Ident {
625696
inner: imp::Ident,
@@ -655,6 +726,11 @@ impl Ident {
655726
///
656727
/// Due to the current importance of hygiene this constructor, unlike other
657728
/// tokens, requires a `Span` to be specified at construction.
729+
///
730+
/// # Panics
731+
///
732+
/// Panics if the input string is neither a keyword nor a legal variable
733+
/// name.
658734
pub fn new(string: &str, span: Span) -> Ident {
659735
Ident::_new(imp::Ident::new(string, span.inner))
660736
}

0 commit comments

Comments
 (0)