strtoint

Crates.iostrtoint
lib.rsstrtoint
version0.1.0
sourcesrc
created_at2022-11-04 20:02:05.740097
updated_at2022-11-05 21:15:34.325781
descriptionParse integers from strings, with support for base prefixes
homepage
repositoryhttps://github.com/jwodder/strtoint
max_upload_size
id705429
size40,912
John T. Wodder II (jwodder)

documentation

README

Project Status: Active – The project has reached a stable, usable state and is being actively developed. CI Status codecov.io MIT License

GitHub | crates.io | Documentation | Issues | Changelog

strtoint provides a function of the same name for parsing integer literals from strings, with support for the base prefixes 0x, 0o, and 0b for hexadecimal, octal, and binary literals, respectively.

This crate supports parsing into all primitive integer types built into Rust, along with their "NonZero" equivalents.

If the std feature (enabled by default) is disabled, this crate will be built in no-std mode. The only difference is that StrToIntError only implements the std::error::Error trait under std.

Installation

strtoint requires version 1.56 of Rust or higher. To use the strtoint library in your Cargo project, add the following to your Cargo.toml:

[dependencies]
strtoint = "0.1.0"

Examples

use core::num::NonZeroUsize;
use strtoint::strtoint;

assert_eq!(strtoint::<i32>("123").unwrap(), 123);
assert_eq!(strtoint::<u32>("0xabcd_FFFF").unwrap(), 2882404351);
assert_eq!(strtoint::<i16>("0o644").unwrap(), 420);
assert_eq!(strtoint::<i8>("-0b00101010").unwrap(), -42);
assert!(strtoint::<i64>("42.0").is_err());

assert_eq!(
    strtoint::<NonZeroUsize>("123_456").unwrap(),
    NonZeroUsize::new(123456).unwrap()
);
assert!(strtoint::<NonZeroUsize>("0").is_err());
Commit count: 39

cargo fmt