You are viewing a free preview of this lesson.
Subscribe to unlock all 10 lessons in this course and every other course on LearningBro.
Rust takes a unique approach to error handling — there are no exceptions. Instead, Rust uses the Result<T, E> enum for recoverable errors and panic! for unrecoverable errors. This makes error handling explicit and impossible to ignore.
| Kind | Mechanism | Example |
|---|---|---|
| Unrecoverable | panic! macro | Index out of bounds, corrupted state |
| Recoverable | Result<T, E> enum | File not found, invalid input, network timeout |
Most errors in Rust are recoverable and should use Result.
When something goes catastrophically wrong:
fn main() {
panic!("crash and burn!");
}
When a panic! occurs:
panic! explicitly.unwrap() on a None or ErrTip: Set the environment variable
RUST_BACKTRACE=1to see a full backtrace when a panic occurs.
The Result enum represents either success or failure:
Subscribe to continue reading
Get full access to this lesson and all 10 lessons in this course.