liveBook · Manning

ParseIntError

As explained in Learn Rust in a Month of Lunches

ParseIntError is the Rust standard-library error type returned when text cannot be parsed as an integer.

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

How Learn Rust in a Month of Lunches covers it

How deep it goes
Defines the error’s role and shows its output, but stops short of detailing its internal structure beyond InvalidDigit.
Examples
Uses strings such as "8", "Seven", "9.0", "nice", and "6060" to demonstrate successful and failed parsing.
What it assumes
Assumes basic familiarity with parsing and Result while teaching how the compiler reveals ParseIntError’s fully qualified type.
What you can do after
Apply ParseIntError by declaring Result error types and forwarding parse failures with the ? operator.
How it connects
Connects ParseIntError to integer parsing, Result, the ? operator, and early returns.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn Rust in a Month of Lunches. The section links point to the parts of the book it draws on.