A lightweight crate for parsing numeric strings with radix-indicating prefixes like 0x, 0o, and 0b, or any other custom prefix, supporting any type that implements num_traits::Num. This includes all base rust numeric types.
The implementation relies on from_str_radix internally, so the same limitations and capabilities present there apply.
- ✅ Parse hexadecimal (
0x), octal (0o), binary (0b), and decimal numbers. - ✅ Support for custom prefix formats and arbitrary radices.
- ✅ Works with any type implementing Num, including
u32,i64,u8,usize, etc.
Handles, hexadecimal (0x), octal (0o), binary (0b), and decimal numbers ('').
use prefix_parse::PrefixParse;
assert_eq!(u32::parse("0x10"), Ok(16));
assert_eq!(u32::parse("0o10"), Ok(8));
assert_eq!(u32::parse("0b10"), Ok(2));
assert_eq!(u32::parse("10"), Ok(10));Handles preconfigured prefixes.
use prefix_parse::{PrefixParse, HEX, OCT, BIN, DEC};
assert_eq!(u32::parse_with(&HEX, "0xFF"), Ok(255));
assert_eq!(u32::parse_with(&OCT, "0o77"), Ok(63));
assert_eq!(u32::parse_with(&BIN, "0b1010"), Ok(10));
assert_eq!(u32::parse_with(&DEC, "42"), Ok(42));Allows definition of custom prefixes.
use prefix_parse::{PrefixFmt, PrefixParse};
let base36 = PrefixFmt {
prefix: "0z",
radix: 36,
};
assert_eq!(u32::parse_with(&base36, "0z1jz"), Ok(2015));Any new type that implements Num from num_traits will automatically implement this.
MIT or Apache-2.0 — your choice.
Contributions welcome!