Panic versus recoverable failure
A panic unwinds the thread and prints a message. It is for bugs — states your program should never reach — not for expected failures like a missing file or bad user input.
`unwrap` panics with a generic message; `expect` panics with yours. Prefer `expect` even in throwaway code, because the message is what you will read at 3am.
The rule of thumb: return a `Result` when the caller could reasonably do something about it, and panic when continuing would mean running on data you know is wrong.
unwrap on real-world input
fn main() {
let input = "not a number";
let value: i32 = input.parse().unwrap(); // panics
println!("{value}");
}thread 'main' panicked at: called `Result::unwrap()` on an `Err` value: ParseIntError { kind: InvalidDigit }
Handle it: `match input.parse::<i32>()`, or `unwrap_or(0)`, or propagate with `?`. Save `unwrap` for cases that genuinely cannot fail.
Run it and change it
The editor below is live: edit anything and the real compiler output updates by itself.