Struct LitStr

struct LitStr { ... }

A UTF-8 string literal: "foo".

Implementations

impl LitStr

fn new(value: &str, span: Span) -> Self
fn value(self: &Self) -> String
fn parse<T: Parse>(self: &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: &Self, parser: F) -> Result<<F as >::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: &Self) -> Span
fn set_span(self: &mut Self, span: Span)
fn suffix(self: &Self) -> &str
fn token(self: &Self) -> Literal

impl Clone for LitStr

fn clone(self: &Self) -> Self

impl Debug for LitStr

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

impl Eq for LitStr

impl Freeze for LitStr

impl Hash for LitStr

fn hash<H>(self: &Self, state: &mut H)
where
    H: Hasher

impl Parse for LitStr

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

impl PartialEq for LitStr

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

impl RefUnwindSafe for LitStr

impl Send for LitStr

impl Sync for LitStr

impl ToTokens for LitStr

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

impl Token for LitStr

impl Unpin for LitStr

impl UnsafeUnpin for LitStr

impl UnwindSafe for LitStr

impl<T> Any for LitStr

fn type_id(self: &Self) -> TypeId

impl<T> Borrow for LitStr

fn borrow(self: &Self) -> &T

impl<T> BorrowMut for LitStr

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

impl<T> CloneToUninit for LitStr

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

impl<T> From for LitStr

fn from(t: T) -> T

Returns the argument unchanged.

impl<T> Spanned for LitStr

fn span(self: &Self) -> Span

impl<T> ToOwned for LitStr

fn to_owned(self: &Self) -> T
fn clone_into(self: &Self, target: &mut T)

impl<T, U> Into for LitStr

fn into(self: 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 for LitStr

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

impl<T, U> TryInto for LitStr

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