This is a reference to help answer this question: โ€œwhat will my Rust program print if something goes wrong?โ€

The examples were compiled with Rust 1.76.0. All output in these examples goes to stderr, nothing goes to stdout.

Setup code for std::io::Error

fn make_error() -> Result<(), std::io::Error> {
    std::fs::remove_file("/this/file/does/not/exist")
}

Debug

fn main() {
    eprintln!("{:?}", make_error().unwrap_err())
}
Os { code: 2, kind: NotFound, message: "No such file or directory" }

Display

fn main() {
    eprintln!("{}", make_error().unwrap_err())
}
No such file or directory (os error 2)

Alternate Display

fn main() {
    eprintln!("{:#}", make_error().unwrap_err())
}
No such file or directory (os error 2)

Unwrap

fn main() {
    make_error().unwrap();
}
thread 'main' panicked at src/bin/io_unwrap.rs:6:18:
called `Result::unwrap()` on an `Err` value: Os { code: 2, kind: NotFound, message: "No such file or directory" }
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

Expect

fn main() {
    make_error().expect("oh no");
}
thread 'main' panicked at src/bin/io_expect.rs:6:18:
oh no: Os { code: 2, kind: NotFound, message: "No such file or directory" }
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

Return

fn main() -> Result<(), std::io::Error> {
    make_error()
}
Error: Os { code: 2, kind: NotFound, message: "No such file or directory" }