Result<T, E> is Ok or Err. The ? operator returns early on Err, keeping the happy path clean.

Program

Play the program to parse and sum comma-separated numbers.

result.rs
fn main() {
    match parse_total("10,20,30") {
        Ok(total) => println!("{total}"),
        Err(e) => println!("error: {e}"),
    }
}

fn parse_total(input: &str) -> Result<i32, std::num::ParseIntError> {
    let mut total = 0;
    for part in input.split(',') {
        total += part.parse::<i32>()?;
    }
    Ok(total)
}
Result `Result<T, E>` reports success (`Ok`) or failure (`Err`).
? operator `parse::<i32>()?` returns early if parsing fails.
Ok `Ok(total)` wraps the successful result.