strtoint 0.1.0

Parse integers from strings, with support for base prefixes
Documentation
  • Coverage
  • 72.73%
    8 out of 11 items documented1 out of 5 items with examples
  • Size
  • Source code size: 42.96 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 2.06 MB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 9s Average build duration of successful builds.
  • all releases: 9s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • jwodder/strtoint
    2 0 7
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • jwodder

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());