Option and Result in practice
Rust has no null. A value that might be absent is an `Option<T>`, and one that might have failed is a `Result<T, E>`. Both are ordinary enums, which means the compiler forces you to consider the missing case.
You rarely need a full `match`. `unwrap_or` supplies a fallback, `map` transforms the success case, `and_then` chains another fallible step, and `ok_or` turns an absent value into a real error.
The payoff is that the unhappy path is visible in the type signature instead of hidden in documentation.
Run it and change it
The editor below is live: edit anything and the real compiler output updates by itself.
Contrast AAA · 16.6:1
15px