Start with the idea
Pattern matching and the ? operator keep both cases explicit without null, sentinel values, or exceptions. Reach for Option when absence is normal and expected, and Result when the caller needs to know why something did not work.
Read the code
use std::num::ParseIntError;
fn parse_port(
raw: &str,
) -> Result<u16, ParseIntError> {
let port: u16 = raw.parse()?;
Ok(port)
}? returns early on the error case, keeping the happy path flat.
Practice starter
fn find(id: u32) -> u32 {
// nothing found is normal here; 0 is a sentinel
if id == 7 { 70 } else { 0 }
}
Work through it
Ask what the caller needs to know when there is no value. If a search simply finds nothing, Option<T> describes the two normal outcomes: Some(item) or None. If a parser fails, Result<T, E> describes success plus a reason for failure. Neither type is a disguised boolean. They carry the successful value, and Result preserves information needed to explain or recover from the error.
Use match when both outcomes deserve visible behavior. if let Some(item) = lookup() is compact when you only care about the present case. map transforms a value inside Some or Ok without inventing a value for the other case. unwrap_or supplies a deliberate default; use it only when the default is genuinely correct. ? can return early for an absent Option or an error Result when the surrounding function has the appropriate return type. A function returning Result cannot use ? on an Option without explicitly choosing what error absence means.
For the exercise, make find(id) return Option<Item> because missing data is expected. Make parse_id(raw) return Result<u32, ParseIntError> because malformed input has a reason. Combine them in a caller: parse first, then look up, and show distinct messages for malformed and unknown IDs. Avoid unwrap() on both paths; it collapses two normal situations into a panic. Good types help the caller write a useful interface.
Option<T> answers whether a value is present. Some(T) carries the value; None means absence. Result<T, E> answers whether an operation succeeded. Ok(T) carries success; Err(E) carries a reason for failure. They are different questions. A search that finds no item may return None; a failed file read needs an error that explains what went wrong. Choosing the right type tells callers what decision to make.
Take a lookup function and test three cases: item found, item missing, and storage unavailable. If the last case is possible, a single Option cannot communicate it well; consider Result<Option<T>, E>. Unwrap only when you have proved the variant or intentionally want to crash on an invariant violation. Prefer match, if let, or combinators when they keep both paths visible. Ask what information a caller needs at the boundary, then choose the return type.
Try it
Keep missing input distinct from invalid input: None becomes Ok(None), a valid integer becomes Ok(Some(n)), and invalid text becomes Err(invalid). A match is fine; transpose is an optional extension. In the Crabrix app, open the compiler lab and complete the supplied function in solution.rs. The reading question is a separate exercise.
What success looks like: The local compiler accepts the changed solution and the separate verification harness finishes all normal and boundary cases. Printing a success message alone does not complete this lab.
Check your understanding
Which return type fits 'the item may simply not exist'?
Reveal the answer
- Result<T, E>
- Option<T>
- A panic
There is no error to report — absence is the expected outcome.
Learn this lesson with AI
Paste this into Claude, Codex, Cursor or another agent connected to Crabrix Academy. It contains only this lesson’s public details.
I'm learning Rust with Crabrix Academy. Teach me this lesson with the Crabrix Academy tools. Lesson: Option & Result (courseId "basics", lessonId "option-result") Link: https://crabrix.com/learn/basics/option-result/ Goal: Option for absence, Result when the caller needs the reason. Practice: open the starter in the Crabrix Playground (https://crabrix.com/playground/?lesson=basics/option-result) and I will write the code there. Explain it step by step for my level, let me write the code myself, give hints before any solution, and do not mark anything as passed: only Crabrix checks do that.