How to Propagate Errors from main() in Rust

Propagate errors from main() in Rust by returning Result<(), E> and using the ? operator or expect().

Use the ? operator to immediately return the error from main() if a function call fails, or wrap the error handling logic in a separate function and return a Result.

use std::fs::File;
use std::io::Error;

fn main() -> Result<(), Error> {
    let file = File::open("hello.txt")?;
    Ok(())
}

Alternatively, use panic! to stop execution on unrecoverable errors:

fn main() {
    let file = File::open("hello.txt").expect("Failed to open file");
}