The ? operator returns early from a function when a Result is an error, and unwraps the success value otherwise.

Program

Play the program to double a selected input or report that parsing failed.

question_mark.rs
fn double_input(text: &str) -> Result<i32, std::num::ParseIntError> {
    let value = text.parse::<i32>()?;
    Ok(value * 2)
}

fn main() {
    let input = ;
    let result = double_input(input);
    let label = match result {
        Ok(value) => format!("double:{value}"),
        Err(_) => "parse error".to_string(),
    };
    println!("{label}");
}
fn double_input(text: &str) -> Result<i32, std::num::ParseIntError> {
    let value = text.parse::<i32>()?;
    Ok(value * 2)
}

fn main() {
    let input = ;
    let result = double_input(input);
    let label = match result {
        Ok(value) => format!("double:{value}"),
        Err(_) => "parse error".to_string(),
    };
    println!("{label}");
}
fn double_input(text: &str) -> Result<i32, std::num::ParseIntError> {
    let value = text.parse::<i32>()?;
    Ok(value * 2)
}

fn main() {
    let input = ;
    let result = double_input(input);
    let label = match result {
        Ok(value) => format!("double:{value}"),
        Err(_) => "parse error".to_string(),
    };
    println!("{label}");
}
? `?` returns the error immediately or gives back the success value.
propagation The parse error flows back to the caller as `Err`.
caller match The caller decides how to turn the result into user-facing text.