Coding/Learn/rust/enums and option
Rust Foundational Course
01.Introduction to Rust & Memory Safety
02.Formatting, Comments & Macros (println!)
03.Variables, Mutability & Shadowing
04.Scalar & Compound Types (Integers, Floats, Tuples, Arrays)
05.Functions, Parameters & Return Expressions
06.Conditionals & If as an Expression
07.Ownership Rules & Move Semantics
08.References & Borrowing (&T)
09.Mutable References (&mut) & Aliasing XOR Mutability
10.Slices & String Types (&str vs String)
11.Loops in Rust (loop, while, for)
12.Pattern Matching with match Expressions
13.Structs, Associated Functions & Methods (impl)
14.Enums & the Option<T> Type (No Null!)
15.Concise Pattern Matching (if let & let else)
16.Vectors & Growable Collections (Vec<T>)
17.Error Handling with Result<T, E> & the ? Operator
18.Traits, Shared Behavior & Generics
19.Closures & Functional Iterators (map, filter, collect)
20.Capstone: High-Performance In-Memory Log Telemetry Engine
Course Certificate0%

Complete all lessons to earn

LearnEnums & the Option<T> Type (No Null!)
14 / 20 0%
Lesson 14 of 20
6 mins read

Enums & the Option<T> Type (No Null!)

PreviousNext Lesson
In Plain English

Rust enums are algebraic data types that can carry distinct data in each variant. Rust has no null reference; instead, the standard library provides Option<T> with variants Some(T) and None to represent presence or absence safely.

The Mental Model

"In other languages, null is like a hidden landmine in a field of flowers. In Rust, Option<T> is a sealed gift box: it is either clearly labeled Some(gift) or None (empty box), and the compiler forces you to inspect the label before opening it."

Deep Dive: How It Works

Data-Bearing Enums: enum WebEvent { Click { x: i64, y: i64 }, KeyPress(char) }

The Option<T> Enum: enum Option<T> { Some(T), None }. Included automatically in standard prelude.

Forced Safety: You cannot access inner value T without explicitly handling the None variant.

Syntax Blueprint

match maybe_value { Some(v) => println!("{}", v), None => println!("Empty") }

Forces complete handling of missing data at compile time, eliminating null pointer exceptions.

PointCore Rules to Remember

Point star
Zero Null Bugs: Rust completely eliminates null pointer dereferences at compile time.
Point star
Data-Bearing Enums: Variants can hold different types, structs, and payloads.
Point star
Pattern Matching: Extract Some(T) payloads safely using match or if let.

Common Beginner Traps & How to Fix Them

Mistake:Calling .unwrap() on a None value.

Why it happens: unwrap() extracts Some(v) but panics at runtime if the value is None.

How to fix: Use match, if let, or .unwrap_or(default) for safe fallbacks.

Pro Tip: Use .unwrap_or(default_value) or .map(|v| v * 2) to transform Option types concisely.

Live Interactive Example

Hit Run Code to see it live
Option Inspection and Custom Enum
Rust 1.78 / rustc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
Output Console
Click "Run Code" to view the rendered output.
How it works: Demonstrates data-bearing enums and safe Option handling with unwrap_or fallback.

Your Turn: Micro Challenge

No pressure! Edit the starter code below and test your solution with instant feedback.

Micro Exercise

Unpack Option with match

  • PointWrite fn find_user(id: i32) -> Option<&'static str> that returns Some("Alex") if id == 1 else None.
  • PointIn main(), match find_user(1) and print "Found User: Alex".
1
2
3
4
5
6
7
8
9
Sandbox Output
Click "Run & Check" to test your solution.

Finished reading and practicing?

Mark this lesson as completed to update your course progress.

PreviousNext Lesson
Overview100 SheetLearnCertificates