pub fn parse_int(s: &str) -> Option<usize>
Expand description
Parses a string representing a number and returns it as a usize
.
The input string can be in decimal or hexadecimal format:
- Decimal numbers are parsed normally (e.g.,
"123"
). - Hexadecimal numbers must be prefixed with
"0x"
(e.g.,"0x7B"
). - Underscores can be used as visual separators (e.g.,
"1_000"
or"0x1_F4"
).
If the input string is not a valid number in the specified format, None
is returned.
§Examples
use forc_debug::cli::parse_int;
/// Use underscores as separators in decimal and hexadecimal numbers
assert_eq!(parse_int("123"), Some(123));
assert_eq!(parse_int("1_000"), Some(1000));
/// Parse hexadecimal numbers with "0x" prefix
assert_eq!(parse_int("0x7B"), Some(123));
assert_eq!(parse_int("0x1_F4"), Some(500));
/// Handle invalid inputs gracefully
assert_eq!(parse_int("abc"), None);
assert_eq!(parse_int("0xZZZ"), None);
assert_eq!(parse_int(""), None);
§Errors
Returns None
if the input string contains invalid characters,
is not properly formatted, or cannot be parsed into a usize
.