https://github.com/jwodder/strtoint
Parse integers from strings, with support for base prefixes
https://github.com/jwodder/strtoint
available-on-crates-io binary hexadecimal int octal parse-int rust
Last synced: 3 months ago
JSON representation
Parse integers from strings, with support for base prefixes
- Host: GitHub
- URL: https://github.com/jwodder/strtoint
- Owner: jwodder
- License: mit
- Created: 2022-11-04T16:01:57.000Z (over 2 years ago)
- Default Branch: master
- Last Pushed: 2025-02-09T21:51:00.000Z (5 months ago)
- Last Synced: 2025-04-12T20:10:03.598Z (3 months ago)
- Topics: available-on-crates-io, binary, hexadecimal, int, octal, parse-int, rust
- Language: Rust
- Homepage:
- Size: 56.6 KB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 8
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
[](https://www.repostatus.org/#active)
[](https://github.com/jwodder/strtoint/actions/workflows/test.yml)
[](https://codecov.io/gh/jwodder/strtoint)
[](https://www.rust-lang.org)
[](https://opensource.org/licenses/MIT)[GitHub](https://github.com/jwodder/strtoint) | [crates.io](https://crates.io/crates/strtoint) | [Documentation](https://docs.rs/strtoint) | [Issues](https://github.com/jwodder/strtoint/issues) | [Changelog](https://github.com/jwodder/strtoint/blob/master/CHANGELOG.md)
`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`.Examples
========```rust
use core::num::NonZeroUsize;
use strtoint::strtoint;assert_eq!(strtoint::("123").unwrap(), 123);
assert_eq!(strtoint::("0xabcd_FFFF").unwrap(), 2882404351);
assert_eq!(strtoint::("0o644").unwrap(), 420);
assert_eq!(strtoint::("-0b00101010").unwrap(), -42);
assert!(strtoint::("42.0").is_err());assert_eq!(
strtoint::("123_456").unwrap(),
NonZeroUsize::new(123456).unwrap()
);
assert!(strtoint::("0").is_err());
```