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 anyhow

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

Debug

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

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/anyhow_unwrap.rs:6:18:
called `Result::unwrap()` on an `Err` value: No such file or directory (os error 2)
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/anyhow_expect.rs:6:18:
oh no: No such file or directory (os error 2)
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

Return

fn main() -> Result<(), anyhow::Error> {
    make_error()
}
Error: No such file or directory (os error 2)