Why Option exists

Other languages use `null`, `nil`, or `None` as a magic value of every reference type. The compiler can't tell whether a given variable might be null at any point, so the responsibility for checking falls on the programmer — and inevitably, sometimes nobody does, and the program crashes. Rust closes the gap by making absence a separate variant of a generic enum: `Option<T>` is either `Some(T)` or `None`. Every read of a possibly-absent value forces you to handle both cases at compile time.

Combinators over match

Most Option code is shorter and clearer with combinators than with match. `map` transforms `Some(x)` to `Some(f(x))`. `and_then` (sometimes called flatMap) chains a function returning Option. `or_else` provides a fallback Option. `unwrap_or` / `unwrap_or_else` provide a default value. `filter` keeps the value only if a predicate holds. Together they handle nearly every Option pipeline without ever needing a match expression.

For deeper background, see a complete cheat-sheet of std collection complexities for the broader context behind this section.

When to use ? on Option

Inside a function returning `Option<T>`, the `?` operator on an Option short-circuits: `Some(v)` becomes `v`, `None` returns `None` from the surrounding function. This makes pipelines of fallible lookups read top-to-bottom: `let x = lookup(a)?.field?.value?;`.

Option<&T> vs Option<T>

If you don't need ownership, prefer `Option<&T>` for return types — it costs only a pointer's worth of memory and avoids cloning. `as_ref` converts `&Option<T>` to `Option<&T>`; `as_deref` further dereferences `Option<String>` to `Option<&str>`.

For deeper background, see the official Rust API guidelines for module-level design for the broader context behind this section.

Common pitfalls

`unwrap()` panics on None and gives no context — replace it with `expect("why this is unreachable")` at minimum. Comparing `Option<T>` directly with `==` is fine and exhaustive: `if x == Some(value)` is idiomatic. Don't convert Option to Result with `ok_or` if you don't actually need an error type — keep it as Option.