Error Handling · 10 min read · Intermediate
Option, Result & The ? Propagation Operator
Replace null pointers and hidden exceptions with explicit Option<T>, Result<T, E>, and clean ? propagation.
What you'll learn
- Handle absence safely with Option<T> instead of null
- Propagate recoverable errors cleanly using the ? operator
- Reserve panic!/unwrap() for truly unrecoverable invariants or tests
Overview
Replace null pointers and hidden exceptions with explicit Option<T>, Result<T, E>, and clean ? propagation.
Code Example
fn parse_port(raw: &str) -> Result<u16, std::num::ParseIntError> {
let port: u16 = raw.trim().parse()?;
Ok(port)
}
Key Takeaways
- Handle absence safely with
Option<T>instead of null - Propagate recoverable errors cleanly using the ? operator
- Reserve panic!/unwrap() for truly unrecoverable invariants or tests
Quick Check
Check Your Understanding
Score: 0 / 1
1. What does the `?` operator do when applied to a `Result<T, E>` inside a function returning `Result`?
Study Workspace
Finished this lesson?
Your progress is saved locally in your browser, no account needed.