# Struct proc_macro::Literal

1.29.0 · source ·

    pub struct Literal(/* private fields */);

Expand description

A literal string (`"hello"`), byte string (`b"hello"`), character (`'a'`), byte character (`b'a'`), an integer or floating point number with or without a suffix (`1`, `1u8`, `2.3`, `2.3f32`). Boolean literals like `true` and `false` do not belong here, they are `Ident`s.

## Implementations§

source§

### impl Literal

source

#### pub fn u8_suffixed(n: u8) -> Literal

Creates a new suffixed integer literal with the specified value.

This function will create an integer like `1u32` where the integer value specified is the first part of the token and the integral is also suffixed at the end. Literals created from negative numbers might not survive round-trips through `TokenStream` or strings and may be broken into two tokens (`-` and positive literal).

Literals created through this method have the `Span::call_site()` span by default, which can be configured with the `set_span` method below.

source

#### pub fn u16_suffixed(n: u16) -> Literal

Creates a new suffixed integer literal with the specified value.

This function will create an integer like `1u32` where the integer value specified is the first part of the token and the integral is also suffixed at the end. Literals created from negative numbers might not survive round-trips through `TokenStream` or strings and may be broken into two tokens (`-` and positive literal).

Literals created through this method have the `Span::call_site()` span by default, which can be configured with the `set_span` method below.

source

#### pub fn u32_suffixed(n: u32) -> Literal

Creates a new suffixed integer literal with the specified value.

This function will create an integer like `1u32` where the integer value specified is the first part of the token and the integral is also suffixed at the end. Literals created from negative numbers might not survive round-trips through `TokenStream` or strings and may be broken into two tokens (`-` and positive literal).

Literals created through this method have the `Span::call_site()` span by default, which can be configured with the `set_span` method below.

source

#### pub fn u64_suffixed(n: u64) -> Literal

Creates a new suffixed integer literal with the specified value.

This function will create an integer like `1u32` where the integer value specified is the first part of the token and the integral is also suffixed at the end. Literals created from negative numbers might not survive round-trips through `TokenStream` or strings and may be broken into two tokens (`-` and positive literal).

Literals created through this method have the `Span::call_site()` span by default, which can be configured with the `set_span` method below.

source

#### pub fn u128_suffixed(n: u128) -> Literal

Creates a new suffixed integer literal with the specified value.

This function will create an integer like `1u32` where the integer value specified is the first part of the token and the integral is also suffixed at the end. Literals created from negative numbers might not survive round-trips through `TokenStream` or strings and may be broken into two tokens (`-` and positive literal).

Literals created through this method have the `Span::call_site()` span by default, which can be configured with the `set_span` method below.

source

#### pub fn usize_suffixed(n: usize) -> Literal

Creates a new suffixed integer literal with the specified value.

This function will create an integer like `1u32` where the integer value specified is the first part of the token and the integral is also suffixed at the end. Literals created from negative numbers might not survive round-trips through `TokenStream` or strings and may be broken into two tokens (`-` and positive literal).

Literals created through this method have the `Span::call_site()` span by default, which can be configured with the `set_span` method below.

source

#### pub fn i8_suffixed(n: i8) -> Literal

Creates a new suffixed integer literal with the specified value.

This function will create an integer like `1u32` where the integer value specified is the first part of the token and the integral is also suffixed at the end. Literals created from negative numbers might not survive round-trips through `TokenStream` or strings and may be broken into two tokens (`-` and positive literal).

Literals created through this method have the `Span::call_site()` span by default, which can be configured with the `set_span` method below.

source

#### pub fn i16_suffixed(n: i16) -> Literal

Creates a new suffixed integer literal with the specified value.

This function will create an integer like `1u32` where the integer value specified is the first part of the token and the integral is also suffixed at the end. Literals created from negative numbers might not survive round-trips through `TokenStream` or strings and may be broken into two tokens (`-` and positive literal).

Literals created through this method have the `Span::call_site()` span by default, which can be configured with the `set_span` method below.

source

#### pub fn i32_suffixed(n: i32) -> Literal

Creates a new suffixed integer literal with the specified value.

This function will create an integer like `1u32` where the integer value specified is the first part of the token and the integral is also suffixed at the end. Literals created from negative numbers might not survive round-trips through `TokenStream` or strings and may be broken into two tokens (`-` and positive literal).

Literals created through this method have the `Span::call_site()` span by default, which can be configured with the `set_span` method below.

source

#### pub fn i64_suffixed(n: i64) -> Literal

Creates a new suffixed integer literal with the specified value.

This function will create an integer like `1u32` where the integer value specified is the first part of the token and the integral is also suffixed at the end. Literals created from negative numbers might not survive round-trips through `TokenStream` or strings and may be broken into two tokens (`-` and positive literal).

Literals created through this method have the `Span::call_site()` span by default, which can be configured with the `set_span` method below.

source

#### pub fn i128_suffixed(n: i128) -> Literal

Creates a new suffixed integer literal with the specified value.

This function will create an integer like `1u32` where the integer value specified is the first part of the token and the integral is also suffixed at the end. Literals created from negative numbers might not survive round-trips through `TokenStream` or strings and may be broken into two tokens (`-` and positive literal).

Literals created through this method have the `Span::call_site()` span by default, which can be configured with the `set_span` method below.

source

#### pub fn isize_suffixed(n: isize) -> Literal

Creates a new suffixed integer literal with the specified value.

This function will create an integer like `1u32` where the integer value specified is the first part of the token and the integral is also suffixed at the end. Literals created from negative numbers might not survive round-trips through `TokenStream` or strings and may be broken into two tokens (`-` and positive literal).

Literals created through this method have the `Span::call_site()` span by default, which can be configured with the `set_span` method below.

source

#### pub fn u8_unsuffixed(n: u8) -> Literal

Creates a new unsuffixed integer literal with the specified value.

This function will create an integer like `1` where the integer value specified is the first part of the token. No suffix is specified on this token, meaning that invocations like `Literal::i8_unsuffixed(1)` are equivalent to `Literal::u32_unsuffixed(1)`. Literals created from negative numbers might not survive rountrips through `TokenStream` or strings and may be broken into two tokens (`-` and positive literal).

Literals created through this method have the `Span::call_site()` span by default, which can be configured with the `set_span` method below.

source

#### pub fn u16_unsuffixed(n: u16) -> Literal

Creates a new unsuffixed integer literal with the specified value.

This function will create an integer like `1` where the integer value specified is the first part of the token. No suffix is specified on this token, meaning that invocations like `Literal::i8_unsuffixed(1)` are equivalent to `Literal::u32_unsuffixed(1)`. Literals created from negative numbers might not survive rountrips through `TokenStream` or strings and may be broken into two tokens (`-` and positive literal).

Literals created through this method have the `Span::call_site()` span by default, which can be configured with the `set_span` method below.

source

#### pub fn u32_unsuffixed(n: u32) -> Literal

Creates a new unsuffixed integer literal with the specified value.

This function will create an integer like `1` where the integer value specified is the first part of the token. No suffix is specified on this token, meaning that invocations like `Literal::i8_unsuffixed(1)` are equivalent to `Literal::u32_unsuffixed(1)`. Literals created from negative numbers might not survive rountrips through `TokenStream` or strings and may be broken into two tokens (`-` and positive literal).

Literals created through this method have the `Span::call_site()` span by default, which can be configured with the `set_span` method below.

source

#### pub fn u64_unsuffixed(n: u64) -> Literal

Creates a new unsuffixed integer literal with the specified value.

This function will create an integer like `1` where the integer value specified is the first part of the token. No suffix is specified on this token, meaning that invocations like `Literal::i8_unsuffixed(1)` are equivalent to `Literal::u32_unsuffixed(1)`. Literals created from negative numbers might not survive rountrips through `TokenStream` or strings and may be broken into two tokens (`-` and positive literal).

Literals created through this method have the `Span::call_site()` span by default, which can be configured with the `set_span` method below.

source

#### pub fn u128_unsuffixed(n: u128) -> Literal

Creates a new unsuffixed integer literal with the specified value.

This function will create an integer like `1` where the integer value specified is the first part of the token. No suffix is specified on this token, meaning that invocations like `Literal::i8_unsuffixed(1)` are equivalent to `Literal::u32_unsuffixed(1)`. Literals created from negative numbers might not survive rountrips through `TokenStream` or strings and may be broken into two tokens (`-` and positive literal).

Literals created through this method have the `Span::call_site()` span by default, which can be configured with the `set_span` method below.

source

#### pub fn usize_unsuffixed(n: usize) -> Literal

Creates a new unsuffixed integer literal with the specified value.

This function will create an integer like `1` where the integer value specified is the first part of the token. No suffix is specified on this token, meaning that invocations like `Literal::i8_unsuffixed(1)` are equivalent to `Literal::u32_unsuffixed(1)`. Literals created from negative numbers might not survive rountrips through `TokenStream` or strings and may be broken into two tokens (`-` and positive literal).

Literals created through this method have the `Span::call_site()` span by default, which can be configured with the `set_span` method below.

source

#### pub fn i8_unsuffixed(n: i8) -> Literal

Creates a new unsuffixed integer literal with the specified value.

This function will create an integer like `1` where the integer value specified is the first part of the token. No suffix is specified on this token, meaning that invocations like `Literal::i8_unsuffixed(1)` are equivalent to `Literal::u32_unsuffixed(1)`. Literals created from negative numbers might not survive rountrips through `TokenStream` or strings and may be broken into two tokens (`-` and positive literal).

Literals created through this method have the `Span::call_site()` span by default, which can be configured with the `set_span` method below.

source

#### pub fn i16_unsuffixed(n: i16) -> Literal

Creates a new unsuffixed integer literal with the specified value.

This function will create an integer like `1` where the integer value specified is the first part of the token. No suffix is specified on this token, meaning that invocations like `Literal::i8_unsuffixed(1)` are equivalent to `Literal::u32_unsuffixed(1)`. Literals created from negative numbers might not survive rountrips through `TokenStream` or strings and may be broken into two tokens (`-` and positive literal).

Literals created through this method have the `Span::call_site()` span by default, which can be configured with the `set_span` method below.

source

#### pub fn i32_unsuffixed(n: i32) -> Literal

Creates a new unsuffixed integer literal with the specified value.

This function will create an integer like `1` where the integer value specified is the first part of the token. No suffix is specified on this token, meaning that invocations like `Literal::i8_unsuffixed(1)` are equivalent to `Literal::u32_unsuffixed(1)`. Literals created from negative numbers might not survive rountrips through `TokenStream` or strings and may be broken into two tokens (`-` and positive literal).

Literals created through this method have the `Span::call_site()` span by default, which can be configured with the `set_span` method below.

source

#### pub fn i64_unsuffixed(n: i64) -> Literal

Creates a new unsuffixed integer literal with the specified value.

This function will create an integer like `1` where the integer value specified is the first part of the token. No suffix is specified on this token, meaning that invocations like `Literal::i8_unsuffixed(1)` are equivalent to `Literal::u32_unsuffixed(1)`. Literals created from negative numbers might not survive rountrips through `TokenStream` or strings and may be broken into two tokens (`-` and positive literal).

Literals created through this method have the `Span::call_site()` span by default, which can be configured with the `set_span` method below.

source

#### pub fn i128_unsuffixed(n: i128) -> Literal

Creates a new unsuffixed integer literal with the specified value.

This function will create an integer like `1` where the integer value specified is the first part of the token. No suffix is specified on this token, meaning that invocations like `Literal::i8_unsuffixed(1)` are equivalent to `Literal::u32_unsuffixed(1)`. Literals created from negative numbers might not survive rountrips through `TokenStream` or strings and may be broken into two tokens (`-` and positive literal).

Literals created through this method have the `Span::call_site()` span by default, which can be configured with the `set_span` method below.

source

#### pub fn isize_unsuffixed(n: isize) -> Literal

Creates a new unsuffixed integer literal with the specified value.

This function will create an integer like `1` where the integer value specified is the first part of the token. No suffix is specified on this token, meaning that invocations like `Literal::i8_unsuffixed(1)` are equivalent to `Literal::u32_unsuffixed(1)`. Literals created from negative numbers might not survive rountrips through `TokenStream` or strings and may be broken into two tokens (`-` and positive literal).

Literals created through this method have the `Span::call_site()` span by default, which can be configured with the `set_span` method below.

source

#### pub fn f32_unsuffixed(n: f32) -> Literal

Creates a new unsuffixed floating-point literal.

This constructor is similar to those like `Literal::i8_unsuffixed` where the float’s value is emitted directly into the token but no suffix is used, so it may be inferred to be a `f64` later in the compiler. Literals created from negative numbers might not survive rountrips through `TokenStream` or strings and may be broken into two tokens (`-` and positive literal).

##### Panics

This function requires that the specified float is finite, for example if it is infinity or NaN this function will panic.

source

#### pub fn f32_suffixed(n: f32) -> Literal

Creates a new suffixed floating-point literal.

This constructor will create a literal like `1.0f32` where the value specified is the preceding part of the token and `f32` is the suffix of the token. This token will always be inferred to be an `f32` in the compiler. Literals created from negative numbers might not survive rountrips through `TokenStream` or strings and may be broken into two tokens (`-` and positive literal).

##### Panics

This function requires that the specified float is finite, for example if it is infinity or NaN this function will panic.

source

#### pub fn f64_unsuffixed(n: f64) -> Literal

Creates a new unsuffixed floating-point literal.

This constructor is similar to those like `Literal::i8_unsuffixed` where the float’s value is emitted directly into the token but no suffix is used, so it may be inferred to be a `f64` later in the compiler. Literals created from negative numbers might not survive rountrips through `TokenStream` or strings and may be broken into two tokens (`-` and positive literal).

##### Panics

This function requires that the specified float is finite, for example if it is infinity or NaN this function will panic.

source

#### pub fn f64_suffixed(n: f64) -> Literal

Creates a new suffixed floating-point literal.

This constructor will create a literal like `1.0f64` where the value specified is the preceding part of the token and `f64` is the suffix of the token. This token will always be inferred to be an `f64` in the compiler. Literals created from negative numbers might not survive rountrips through `TokenStream` or strings and may be broken into two tokens (`-` and positive literal).

##### Panics

This function requires that the specified float is finite, for example if it is infinity or NaN this function will panic.

source

#### pub fn string(string: &str) -> Literal

String literal.

source

#### pub fn character(ch: char) -> Literal

Character literal.

source

#### pub fn byte_character(byte: u8) -> Literal

🔬This is a nightly-only experimental API. (`proc_macro_byte_character` #115268)

Byte character literal.

source

#### pub fn byte_string(bytes: &[u8]) -> Literal

Byte string literal.

source

#### pub fn span(&self) -> Span

Returns the span encompassing this literal.

source

#### pub fn set_span(&mut self, span: Span)

Configures the span associated for this literal.

source

#### pub fn subspan<R: RangeBounds<usize>>(&self, range: R) -> Option<Span>

🔬This is a nightly-only experimental API. (`proc_macro_span` #54725)

Returns a `Span` that is a subset of `self.span()` containing only the source bytes in range `range`. Returns `None` if the would-be trimmed span is outside the bounds of `self`.

## Trait Implementations§

source§

### impl Clone for Literal

source§

#### fn clone(&self) -> Literal

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 Literal

source§

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

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

### impl Display for Literal

Prints the literal as a string that should be losslessly convertible back into the same literal (except for possible rounding for floating point literals).

source§

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

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

### impl From<Literal> for TokenTree

source§

#### fn from(g: Literal) -> TokenTree

Converts to this type from the input type.
1.54.0 · source§

### impl FromStr for Literal

Parse a single literal from its stringified representation.

In order to parse successfully, the input string must not contain anything but the literal token. Specifically, it must not contain whitespace or comments in addition to the literal.

The resulting literal token will have a `Span::call_site()` span.

NOTE: some errors may cause panics instead of returning `LexError`. We reserve the right to change these errors into `LexError`s later.

§

#### type Err = LexError

The associated error which can be returned from parsing.
source§

#### fn from_str(src: &str) -> Result<Self, LexError>

Parses a string `s` to return a value of this type. Read more

## Auto Trait Implementations§

§

### impl RefUnwindSafe for Literal

§

### impl !Send for Literal

§

### impl !Sync for Literal

§

### impl Unpin for Literal

§

### impl UnwindSafe for Literal

## Blanket Implementations§

source§

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

source§

#### fn type_id(&self) -> TypeId

Gets the `TypeId` of `self`. Read more
source§

### impl<T> Borrow<T> for Twhere T: ?Sized,

source§

#### fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

### impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

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

Mutably borrows from an owned value. 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 Twhere 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> ToOwned for Twhere T: Clone,

§

#### 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 Twhere T: Display + ?Sized,

source§

#### default fn to_string(&self) -> String

Converts the given value to a `String`. Read more
source§

### impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

#### 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 Twhere U: TryFrom<T>,

§

#### 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.