Error Propagation with ?

Rust provides the ? operator to simplify error propagation.

Example

use std::fs::File;

fn read_file() -> Result<(), std::io::Error> {
    let f = File::open("hello.txt")?;
    Ok(())
}

? returns the error to the calling function if one occurs.

It works with any type that implements the From trait for error conversion.

This makes your code cleaner and easier to read.

← PrevNext →