#[non_exhaustive]pub struct IntegerLit<B: Buffer> {
raw: B,
start_main_part: usize,
end_main_part: usize,
base: IntegerBase,
}
Expand description
An integer literal, e.g. 27
, 0x7F
, 0b101010u8
or 5_000_000i64
.
An integer literal consists of an optional base prefix (0b
, 0o
, 0x
),
the main part (digits and underscores), and an optional type suffix
(e.g. u64
or i8
). See the reference for more information.
Note that integer literals are always positive: the grammar does not contain
the minus sign at all. The minus sign is just the unary negate operator,
not part of the literal. Which is interesting for cases like - 128i8
:
here, the literal itself would overflow the specified type (i8
cannot
represent 128). That’s why in rustc, the literal overflow check is
performed as a lint after parsing, not during the lexing stage. Similarly,
IntegerLit::parse
does not perform an overflow check.
Fields (Non-exhaustive)§
This struct is marked as non-exhaustive
Struct { .. }
syntax; cannot be matched against without a wildcard ..
; and struct update syntax will not work.raw: B
The raw literal. Grammar: <prefix?><main part><suffix?>
.
start_main_part: usize
First index of the main number part (after the base prefix).
end_main_part: usize
First index not part of the main number part.
base: IntegerBase
Parsed raw[..start_main_part]
.
Implementations§
source§impl<B: Buffer> IntegerLit<B>
impl<B: Buffer> IntegerLit<B>
sourcepub fn parse(input: B) -> Result<Self, ParseError>
pub fn parse(input: B) -> Result<Self, ParseError>
Parses the input as an integer literal. Returns an error if the input is invalid or represents a different kind of literal.
sourcepub fn value<N: FromIntegerLiteral>(&self) -> Option<N>
pub fn value<N: FromIntegerLiteral>(&self) -> Option<N>
Performs the actual string to int conversion to obtain the integer
value. The optional type suffix of the literal is ignored by this
method. This means N
does not need to match the type suffix!
Returns None
if the literal overflows N
.
Hint: u128
can represent all possible values integer literal values,
as there are no negative literals (see type docs). Thus you can, for
example, safely use lit.value::<u128>().to_string()
to get a decimal
string. (Technically, Rust integer literals can represent arbitrarily
large numbers, but those would be rejected at a later stage by the Rust
compiler).
sourcepub fn base(&self) -> IntegerBase
pub fn base(&self) -> IntegerBase
The base of this integer literal.
sourcepub fn raw_main_part(&self) -> &str
pub fn raw_main_part(&self) -> &str
The main part containing the digits and potentially _
. Do not try to
parse this directly as that would ignore the base!
sourcepub fn suffix(&self) -> &str
pub fn suffix(&self) -> &str
The optional suffix. Returns ""
if the suffix is empty/does not exist.
If you want the type, try IntegerType::from_suffix(lit.suffix())
.
sourcepub fn into_raw_input(self) -> B
pub fn into_raw_input(self) -> B
Returns the raw input that was passed to parse
, potentially owned.
source§impl IntegerLit<&str>
impl IntegerLit<&str>
sourcepub fn to_owned(&self) -> IntegerLit<String>
pub fn to_owned(&self) -> IntegerLit<String>
Makes a copy of the underlying buffer and returns the owned version of
Self
.
Trait Implementations§
source§impl<B: Clone + Buffer> Clone for IntegerLit<B>
impl<B: Clone + Buffer> Clone for IntegerLit<B>
source§fn clone(&self) -> IntegerLit<B>
fn clone(&self) -> IntegerLit<B>
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moresource§impl<B: Buffer> Display for IntegerLit<B>
impl<B: Buffer> Display for IntegerLit<B>
source§impl<B: Buffer> From<IntegerLit<B>> for Literal
impl<B: Buffer> From<IntegerLit<B>> for Literal
source§fn from(l: IntegerLit<B>) -> Self
fn from(l: IntegerLit<B>) -> Self
source§impl<B: Buffer> From<IntegerLit<B>> for Literal<B>
impl<B: Buffer> From<IntegerLit<B>> for Literal<B>
source§fn from(src: IntegerLit<B>) -> Self
fn from(src: IntegerLit<B>) -> Self
impl<B: Copy + Buffer> Copy for IntegerLit<B>
impl<B: Eq + Buffer> Eq for IntegerLit<B>
impl<B: Buffer> StructuralPartialEq for IntegerLit<B>
Auto Trait Implementations§
impl<B> Freeze for IntegerLit<B>where
B: Freeze,
impl<B> RefUnwindSafe for IntegerLit<B>where
B: RefUnwindSafe,
impl<B> Send for IntegerLit<B>where
B: Send,
impl<B> Sync for IntegerLit<B>where
B: Sync,
impl<B> Unpin for IntegerLit<B>where
B: Unpin,
impl<B> UnwindSafe for IntegerLit<B>where
B: UnwindSafe,
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
source§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)