Unrecoverable Errors with panic!
Sometimes your program encounters a situation it simply cannot handle (like an index out of bounds). In these cases, Rust uses the
panic! macro to stop execution immediately and print a failure message.rust
Recoverable Errors with Result
For most errors, you don't want to stop the program. Rust uses the
Result<T, E> enum for errors that you expect might happen.rust
rust
The ? Operator (Propagating Errors)
Wait! Writing
match for every error is exhausting. Rust provides the ? operator as a shortcut. If the result is an Ok, the value is returned. If it's an Err, the entire function returns early and passes the error to the caller.rust
Option for Missing Values
If a value might simply be missing (not necessarily an "error"), use
Option<T>.rust
unwrap and expect
If you are 100% sure a value is present, you can use
.unwrap() or .expect("custom message") to get the value. If the value is missing, the program will panic.[!WARNING] Useunwrapsparingly. It's great for quick prototyping or tests, but in production code, it's safer to handle the error properly withmatchor?.
Summary
| Term | Usage | Description |
|---|---|---|
| panic! | Fatal | Stop the program immediately |
| Result | Recoverable | Ok(value) or Err(error) |
| Option | Optional | Some(value) or None |
| ? | Propagate | Return error to caller automatically |
| unwrap | Shortcut | Get value or panic |
| expect | Shortcut | Get value or panic with message |
| match | Handle | Explicitly deal with every case |
| Safety | No "checked exceptions" or "null" pointers | |
| Clean | Chain methods like .map() and .and_then() | |
| Custom | Create your own error types using structs and enums | |
| Thiserror | Popular crate for creating custom error types | |
| Anyhow | Popular crate for easy error handling in applications | |
| Standard | From trait allows automatic conversion of errors | |
| Identity | An error never "forgets" its real cause | |
| Refactor | Replacing unwrap with ? is a sign of Rust maturity |