Option & Result
Rust is a systems programming language focused on speed, memory safety, and fearless concurrency — and it has no null and no exceptions. Instead, absence and failure are modelled with two enums: Option and Result .
Learn Option & Result in our free Rust course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a quick reference.
Part of the free Rust course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.
In this lesson you'll use Option<T> for maybe-missing values and Result<T, E> for operations that can fail, and handle both safely.
What You'll Learn in This Lesson
1️⃣ Option: Values That Might Be Missing
Option<T> is an enum with two variants: Some(value) when a value exists and None when it doesn't. Because it's a distinct type, the compiler forces you to deal with the None case before you can touch the value — eliminating null-pointer bugs.
Methods like .get() return an Option precisely because the index might be out of range. Helpers such as unwrap_or let you supply a fallback without a full match.
2️⃣ Result: Operations That Can Fail
Result<T, E> is the failure-aware sibling of Option . It is Ok(value) on success or Err(error) on failure, where the error carries information about what went wrong. Functions that can fail — parsing, dividing, file I/O — return a Result .
The difference from Option : Err tells you why it failed. That's why divide returns a descriptive message instead of just None .
Your turn. Fill in the blanks marked ___ , then run it.
Return an Option<char> and handle both cases. Run it with cargo run .
📋 Quick Reference — Option & Result
Practice quiz
What are the two variants of Option<T>?
- Ok and Err
- Just and Nothing
- Some and None
- True and False
Answer: Some and None. Option is either Some(value) or None.
Why does Rust use Option instead of null?
- The compiler forces you to handle the None case
- It is faster
- It uses less memory
- null is deprecated
Answer: The compiler forces you to handle the None case. Option is a distinct type, so missing values must be handled at compile time.
What does vec![10,20,30].get(99) return?
- 20
- Some(99)
- a panic
- None
Answer: None. .get on an out-of-range index returns None.
What does numbers.get(99).unwrap_or(&-1) return?
- 99
- -1
- None
- a panic
Answer: -1. unwrap_or supplies the fallback -1 when the value is None.
What are the two variants of Result<T, E>?
- Ok and Err
- Some and None
- Pass and Fail
- Yes and No
Answer: Ok and Err. Result is Ok(value) on success or Err(error) on failure.
What is the key difference between Option and Result?
- Option is faster
- Result has no None
- Result carries an error describing the failure
- There is no difference
Answer: Result carries an error describing the failure. Result's Err carries error info; Option's None carries none.
What does "oops".parse::<i32>().unwrap_or(0) return?
- oops
- 0
- None
- a panic
Answer: 0. parse fails, so unwrap_or returns the fallback 0.
Why is calling unwrap() risky?
- It is slow
- It returns null
- It never compiles
- It panics on None or Err
Answer: It panics on None or Err. unwrap panics and crashes the program on a None or Err.
What does Some(5).map(|x| x * 2) produce?
- 10
- Some(10)
- Some(5)
- None
Answer: Some(10). map transforms the inner value while keeping it wrapped as Some.
When should you prefer Result over Option?
- When absence is normal
- Always
- When you need to know why something failed
- Never
Answer: When you need to know why something failed. Use Result when you need the reason for a failure; Option for plain absence.