panic!
What you'll learn:
- tell a panic, which stops the program, from an error the program must handle;
- handle a
Resultwithmatchand take the value out withunwrap,expectandunwrap_or; - pass an error to the calling code with the
?operator; - describe the causes of errors with your own enum.

Picture an office building. People are at work, and suddenly the fire alarm goes off. Nobody finishes what they started: work stops, everyone leaves, and the panel shows which sensor went off.
A program, too, can run into something that, as its author planned it, could never happen. Going on is dangerous: the program will produce a wrong result and not even notice. That is when Rust sets off its own fire alarm, a panic: the program prints a message and stops.
You can trigger a panic yourself with the panic! macro:
fn check_age(age: i32) {
if age < 0 {
panic!("age cannot be negative: {age}");
}
println!("age {age} is ok");
}
fn main() {
check_age(30); // age 30 is ok
check_age(-5); // ✗
check_age(40);
}age cannot be negative: -5Let's walk through the example line by line:
check_age(30): the age is not negative, the condition is false, and the function printsage 30 is ok.check_age(-5): the condition is true, andpanic!stops the program. The text in the parentheses is written the same way as inprintln!,{age}included.check_age(40)never runs: after the panic, the program is no longer running.
What a panic prints
A panic message takes two lines:
- the first says where the alarm went off: the file, the line number and the column;
- the second is the text you passed to
panic!, hereage cannot be negative: -5.
Below them, Rust hints how to see the whole chain of calls that led to the panic: run the program with RUST_BACKTRACE=1 cargo run.
Before stopping, Rust closes every function it started, one after another, and frees their variables, as at a closing }. That is like switching off the lights and shutting the doors during the alarm: the building empties by the rules. After a panic the program exits with code 101, not 0, and other programs learn about the failure from that number.
Panics from the standard library
You are not the only one who sets off the alarm. The standard library panics on its own wherever going on is impossible. You have already seen some of these cases:
- indexing past the end of an array or a vector:
index out of bounds: the len is 3 but the index is 5; - integer overflow in a debug build:
attempt to add with overflow; - a string slice that cuts a letter in half;
- integer division by zero:
attempt to divide by zero.
When a panic fits
A panic is for errors in the program itself, not in its data. If a function receives something its own rules say it can never receive, that is a bug. It is more honest to stop at once than to keep computing with broken data.
But many failures are a normal part of the work:
- the user typed letters instead of a number;
- the file you need is not on the disk;
- the network did not answer.
A program must expect such failures and handle them, not crash. Rust has a different tool for them, the Result type, and nobody sets off the alarm over them.
Here the alarm analogy falls short. After a fire drill, people go back into the building and carry on. After a panic in main, the program is over, and it cannot pick up where it stopped.
Interviewers like to ask this: "How is a panic different from an exception?" An exception is how Java, Python or C++ report an error. You catch it with a try/catch construct, and the program keeps working. Rust has no exceptions and no try/catch. You do not catch a panic to keep working: it reports a bug, while a function returns expected errors as an ordinary value.
Common mistake
The divisor came from outside, and nobody checked it:
fn average(sum: i32, count: i32) -> i32 {
sum / count // ✗
}
fn main() {
println!("{}", average(10, 2)); // 5
println!("{}", average(0, 0));
}attempt to divide by zeroThe program builds and prints 5, then panics on the second call. The compiler sees no error: count is a parameter, and its value is known only when the program runs. Unlike overflow, integer division by zero panics in every build, release included.
The fix is to check the divisor before dividing and return an Option: if count == 0 { None } else { Some(sum / count) }. Then the calling code decides what to show instead of the average.
Key points
- A panic stops the program with a message; you can trigger one with the
panic!macro. - The standard library panics too: an index past the end, overflow in a debug build, division by zero.
- A panic is for bugs in the program, while expected failures such as bad input are returned as a value.
- You do not catch a panic like an exception: after a panic in
main, the program does not carry on.