from_str_radix, ParseIntError::kind
pub fn from_str_radix(src: &str, radix: u32) -> Result<Self, ParseIntError>
pub fn kind(&self) -> &IntErrorKindParse a digit string in a given radix and inspect IntErrorKind on failure. An invalid radix panics; invalid digits return Err.
| Parameter | Description |
|---|---|
src | The digit string, without a radix prefix such as 0x. Sign characters are allowed for signed types. |
radix | The base, from 2 to 36 inclusive. A radix outside that range panics. |
Returns: Ok(value) when the string is valid in that radix and fits in the target type, otherwise Err(ParseIntError). kind() borrows an IntErrorKind.
Errors and panics
ParseIntError— Returned for empty input, invalid digits, or overflow. The example prints Debug of kind(), which is InvalidDigit for "fg" in base 16. This was measured with rustc 1.90.0.panic— from_str_radix panics if radix is not in 2..=36. That case is not executed here.
- See the Option and Result page for decimal parse::<u16>() of a port string, including values that do not fit in u16.
fn main() {
println!("{}", i32::from_str_radix("ff", 16).unwrap());
let err = i32::from_str_radix("fg", 16).unwrap_err();
println!("{:?}", err.kind());
}Expected output
255
InvalidDigitRelated: str::parse and Result, TryFrom for integers