Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

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 unwrap vs propagate errors

Reading time

30 min
+ 20 min exercises
Null vs Type

Hidden Nullability vs Explicit Absence

Null NULL ordinary reference-shaped variable may secretly be invalid Option Some None caller must handle the two states explicitly
Error Flow

What `?` Expands Into

let v = parse()?; Ok(v) unwrap and continue Err(e) From::from(e) return Err(converted) happy path stays linear error path exits early
Decision Flow

Choosing How to Handle a Result or Option

Got a Result / Option? Can you handle it here? Yes match / if let No Should it propagate? Yes ? operator No Need a default? Yes unwrap_or(_else) No .unwrap() prototype / provably safe
Reach for ? 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.

Step 1 - The Problem