ParseIntError is the error type produced when parsing text as an integer fails. Integer parsing returns a Result: successful input gives a number, while invalid input gives Err(ParseIntError). section 6.2
Parsing several strings
Parsing "8" succeeds as Ok(8), while "Seven", "9.0", and "nice" produce Err(ParseIntError { kind: InvalidDigit }). The error can be returned early with the ? operator. section 6.2
use std::num::ParseIntError; #1
fn parse_and_log_str(input: &str) -> Result<i32, ParseIntError> {
let parsed_number = input.parse::<i32>()?; #2
println!("Number parsed successfully into {parsed_number}");
Ok(parsed_number)
}
Use ParseIntError as the error parameter in Result<i32, ParseIntError>; ? forwards parsing failures and Ok(parsed_number) returns success. section 6.2
