Crates.io | strtoint |
lib.rs | strtoint |
version | 0.1.0 |
source | src |
created_at | 2022-11-04 20:02:05.740097 |
updated_at | 2022-11-05 21:15:34.325781 |
description | Parse integers from strings, with support for base prefixes |
homepage | |
repository | https://github.com/jwodder/strtoint |
max_upload_size | |
id | 705429 |
size | 40,912 |
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
.
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"
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());