Chapter 14: Option, Result, and Rust’s Error Philosophy
Prerequisites
You will understand
- Why Rust has no
null - The
?operator as early-return sugar - When to
unwrapvs propagate errors
Reading time
30 min
+ 20 min exercises
Builds on Chapter 13
Option<T> and Result<T, E> are enums. Pattern matching is how you extract the success or failure value — there is no null to check.
Revisit Ch 13 →
Null vs Type
Hidden Nullability vs Explicit Absence
Error Flow
What `?` Expands Into
Decision Flow
Choosing How to Handle a Result or Option
? first in production code — it keeps the happy path linear and makes errors the caller's problem. Use match when you need local recovery. Use .unwrap() only when you can prove the value is always present, or in tests.Watch ? do its job twice: once on the happy path, once on the early return.
Interactive simulation (requires JavaScript): on the happy path the ? operator unwraps Ok(8080) and execution continues; on the failure path it early-returns the Err from the function, which arrives in the caller as an ordinary value that the type system forces the caller to handle.