ParseInt

Trait ParseInt 

Source
pub trait ParseInt: FromStrRadix + TryFrom<u64> {
    // Provided method
    fn from_str(src: &BStr) -> Result<Self> { ... }
}
Expand description

Trait for parsing string representations of integers.

Strings beginning with 0x, 0o, or 0b are parsed as hex, octal, or binary respectively. Strings beginning with 0 otherwise are parsed as octal. Anything else is parsed as decimal. A leading + or - is also permitted. Any string parsed by kstrtol() or kstrtoul() will be successfully parsed.

§Examples


assert_eq!(Ok(0u8), u8::from_str(b_str!("0")));

assert_eq!(Ok(0xa2u8), u8::from_str(b_str!("0xa2")));
assert_eq!(Ok(-0xa2i32), i32::from_str(b_str!("-0xa2")));

assert_eq!(Ok(-0o57i8), i8::from_str(b_str!("-0o57")));
assert_eq!(Ok(0o57i8), i8::from_str(b_str!("057")));

assert_eq!(Ok(0b1001i16), i16::from_str(b_str!("0b1001")));
assert_eq!(Ok(-0b1001i16), i16::from_str(b_str!("-0b1001")));

assert_eq!(Ok(127i8), i8::from_str(b_str!("127")));
assert!(i8::from_str(b_str!("128")).is_err());
assert_eq!(Ok(-128i8), i8::from_str(b_str!("-128")));
assert!(i8::from_str(b_str!("-129")).is_err());
assert_eq!(Ok(255u8), u8::from_str(b_str!("255")));
assert!(u8::from_str(b_str!("256")).is_err());

Provided Methods§

Source

fn from_str(src: &BStr) -> Result<Self>

Parse a string according to the description in Self.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

impl ParseInt for i8

Source§

impl ParseInt for i16

Source§

impl ParseInt for i32

Source§

impl ParseInt for i64

Source§

impl ParseInt for isize

Source§

impl ParseInt for u8

Source§

impl ParseInt for u16

Source§

impl ParseInt for u32

Source§

impl ParseInt for u64

Source§

impl ParseInt for usize

Implementors§