Struct LitStr

pub struct LitStr { /* private fields */ }

A UTF-8 string literal: "foo".

Implementations

impl LitStr

fn new(value: &str, span: Span) -> Self
fn value(&self) -> String
fn parse<T: Parse>(&self) -> Result<T>

Parse a syntax tree node from the content of this string literal.

All spans in the syntax tree will point to the span of this LitStr.

Example

use syn::{Attribute, Error, Expr, Lit, Meta, Path, Result};

// Parses the path from an attribute that looks like:
//
//     #[path = "a::b::c"]
//
// or returns `None` if the input is some other attribute.
fn get_path(attr: &Attribute) -> Result<Option<Path>> {
    if !attr.path().is_ident("path") {
        return Ok(None);
    }

    if let Meta::NameValue(meta) = &attr.meta {
        if let Expr::Lit(expr) = &meta.value {
            if let Lit::Str(lit_str) = &expr.lit {
                return lit_str.parse().map(Some);
            }
        }
    }

    let message = "expected #[path = \"...\"]";
    Err(Error::new_spanned(attr, message))
}
fn parse_with<F: Parser>(&self, parser: F) -> Result<F::Output>

Invoke parser on the content of this string literal.

All spans in the syntax tree will point to the span of this LitStr.

Example

# use proc_macro2::Span;
# use syn::{LitStr, Result};
#
# fn main() -> Result<()> {
#     let lit_str = LitStr::new("a::b::c", Span::call_site());
#
#     const IGNORE: &str = stringify! {
let lit_str: LitStr = /* ... */;
#     };

// Parse a string literal like "a::b::c" into a Path, not allowing
// generic arguments on any of the path segments.
let basic_path = lit_str.parse_with(syn::Path::parse_mod_style)?;
#
#     Ok(())
# }
fn span(&self) -> Span
fn set_span(&mut self, span: Span)
fn suffix(&self) -> &str
fn token(&self) -> Literal

Trait Implementations

impl Clone for LitStr

fn clone(&self) -> Self

impl Parse for LitStr

fn parse(input: ParseStream<'_>) -> Result<Self>

impl ToTokens for LitStr

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

impl Token for LitStr

Auto Trait Implementations

impl !Send for LitStr

impl !Sync for LitStr

impl Freeze for LitStr

impl RefUnwindSafe for LitStr

impl Unpin for LitStr

impl UnsafeUnpin for LitStr

impl UnwindSafe for LitStr

Blanket Implementations

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

fn type_id(&self) -> TypeId

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

fn borrow(&self) -> &T

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

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

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

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

impl<T> From<T> for LitStr

fn from(t: T) -> T

Returns the argument unchanged.

impl<T> Spanned for LitStr where T: Spanned + ?Sized,

fn span(&self) -> Span

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

type Owned = T;
fn to_owned(&self) -> T
fn clone_into(&self, target: &mut T)

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

fn into(self) -> U

Calls U::from(self).

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

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

type Error = never;
fn try_from(value: U) -> Result<T, never>

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

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