Skip to main content
Module 2 · Lesson 2 of 24/24 in the course~20 min
Module lessons (2/2)

Enum, Option<T>, match ed if let

Lesson preview5 hands-on exercises readyCourse Rust · Module 2 — Flow Control & Pattern Matching

Model algebraic states with enum, replace null with Option<T>, check exhaustiveness in match, and simplify with if let.

What you'll learn in this lesson

  • Define algebraic types (enum) whose variants carry different and specific data.
  • Use Option<T> (Some(T) / None) instead of unsafe null pointers.
  • Ensure exhaustiveness of all possible cases with match and reduce the boilerplate using if let.

Overview

Many bugs in programs stem from the classic billion-dollar error: the null pointer. Rust does not have null; instead it uses the standard enumeration Option<T> combined with the exhaustiveness check of match.

Each variant of a enum in Rust can contain a payload with different structure: no fields, a tuple, or nominal fields as a struct:

Sections covered in this lesson

  1. 11. Enum with associated data (Sum Types)
  2. 22. Option<T>: Explicit absence in the Type Stack
  3. 33. Interactive Simulator Option<T> and match
  4. 44. Matching on numbers, guards and if let
  5. 5Practical exercises
  6. 6Check for understanding
Pro Lesson · Module 2 — Flow Control & Pattern Matching

Unlock "Enum, Option<T>, match ed if let" and the full Course Rust curriculum

Model algebraic states with `enum`, replace `null` with `Option<T>`, check exhaustiveness in `match`, and simplify with `if let`.

Unlock all 5 hands-on exercises in this lesson plus every advanced WebAssembly module with automated checks.

  • Instant access to all advanced modules in Course Rust plus 17 more courses
  • Hands-on browser exercises with automated test suites and solutions
  • 14-day money-back guarantee processed via Paddle.com

14-day guarantee · Cancel anytimeModule 1, all 18 interactive games, and cheatsheets are always free.