What is an 'Option'?
Part of the Logic & Flow section of Coddy's Rust journey — lesson 35 of 66.
In programming, you often encounter situations where a value might or might not be present. For example, searching for an item in a list might find the item, or it might not exist at all. Traditional programming languages often use special values like null or -1 to represent "nothing," but this approach can lead to bugs and crashes.
Rust takes a different approach with the Option<T> enum. This is a built-in type that explicitly represents the possibility of absence. Instead of guessing whether a value exists, Rust forces you to handle both cases upfront.
The Option enum has exactly two variants:
Some(value) // Contains a value
None // Contains no valueWhen a function might not return a value, it returns an Option. If there's a value to return, it wraps it in Some. If there's no value, it returns None.
This makes the potential absence explicit in the type system, preventing you from accidentally using a value that doesn't exist.
You've already seen Option in action with hash map's .get() method - it returns Some(&value) when a key exists, or None when it doesn't. This pattern appears throughout Rust, making your code safer and more predictable.
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
Cheat sheet
The Option<T> enum represents a value that might or might not be present, preventing bugs from using non-existent values.
It has two variants:
Some(value) // Contains a value
None // Contains no valueFunctions that might not return a value use Option. For example, hash map's .get() method returns Some(&value) when a key exists, or None when it doesn't.
Try it yourself
This lesson doesn't include a code challenge.
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Logic & Flow
1Advanced Control Flow
The 'match' ExpressionMatching Multiple ValuesMatching RangesThe 'if let' ExpressionLoops as ExpressionsRecap - Simple Command Parser4Grouping Data with Structs
What is a Struct?Structs OverviewAccessing Struct FieldsMutable StructsStructs as Function ParametersTuple StructsRecap - Create a Book Struct7Handling Errors with 'Result'
What is a 'Result'?Using 'match' with 'Result'is_ok() and is_err()Shortcuts: unwrap and expectThe Question Mark Operator '?'Parsing Strings to NumbersRecap - Safe Division Function10Closures & Anonymous Functions
What is a Closure?Defining a Simple ClosureClosures with ParametersCapturing the EnvironmentRecap - Simple Adder Closure2Introduction to Vectors
What is a Vector?Creating a VectorAdding Elements with pushAccessing Vector ElementsIterating Over a VectorMutable IterationRemoving ElementsRecap - Basic Score Tracker5Key-Value Pairs with Hash Maps
What is a Hash Map?Creating a Hash MapInserting Key-Value PairsAccessing ValuesIterating Over a Hash MapUpdating a ValueRemoving a PairRecap - Word Counter8Project: Simple Item Inventory
Project SetupAdding an ItemChecking StockSelling an ItemPutting it all together6Handling Absence with 'Option'
What is an 'Option'?Using 'match' with 'Option'is_some() and is_none()Unwrapping an 'Option'The expectProviding a Default: unwrap_orRecap - Find an Element