konst::primitive

Function parse_u128

Source
pub const fn parse_u128(s: &str) -> Result<u128, ParseIntError>
Expand description

Parses a u128 from a &str.

This returns an Err if the string would not successfully .parse() into a u128.

To parse a u128 from only part of a string, you can use Parser::parse_u128.

§Const stabilization

The equivalent std function, was const-stabilized in Rust 1.82.0.

§Example

use konst::{
    primitive::{ParseIntResult, parse_u128},
    unwrap_ctx,
};

const I: ParseIntResult<u128> = parse_u128("1000");

assert_eq!(I, Ok(1000));
assert_eq!(parse_u128("123"), Ok(123));
assert_eq!(parse_u128("0"), Ok(0));

// This is how you can unwrap integers parsed from strings, at compile-time.
const I2: u128 = unwrap_ctx!(parse_u128("1000"));
assert_eq!(I2, 1000);

assert!(parse_u128("-1").is_err());
assert!(parse_u128("100A").is_err());
assert!(parse_u128("-").is_err());