Coding/Learn/rust/match control flow
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

LearnPattern Matching with match Expressions
12 / 20 0%
Lesson 12 of 20
6 mins read

Pattern Matching with match Expressions

PreviousNext Lesson
In Plain English

The match expression compares a value against a series of patterns and executes code based on which pattern matches. match is exhaustive: the compiler forces you to handle every possible value, eliminating unhandled state bugs.

The Mental Model

"A match expression is like a biometric sorting machine: coins roll down chutes with precision-cut slots. Every single coin denomination has an exact matching slot, with a catch-all tray (_) at the bottom for foreign coins."

Deep Dive: How It Works

Exhaustiveness: You must cover all possible cases (or provide a catch-all _ arm).

Match Arms: pattern => expression, separated by commas.

Match Guards: pattern if condition => expr adds extra boolean constraints.

Syntax Blueprint

match code { 200 => "OK", 404 => "Not Found", _ => "Unknown" }

Evaluates top-to-bottom and returns the resulting expression of the first matching arm.

PointCore Rules to Remember

Point star
Exhaustive: Compiler guarantees zero missed enum variants or numbers.
Point star
Wildcard _: Acts as default fallback for all remaining unhandled patterns.
Point star
Multiple Patterns: Match multiple values in one arm: 1 | 2 | 3 => "Low".

Common Beginner Traps & How to Fix Them

Mistake:Non-exhaustive match on integers or enums.

Why it happens: Matching integers 1, 2 without a wildcard _ causes E0004: non-exhaustive patterns.

How to fix: Always include the wildcard pattern _ => ... for unbounded types.

Pro Tip: You can match ranges directly in match arms: 1..=10 => "Single digit or ten".

Live Interactive Example

Hit Run Code to see it live
HTTP Status Code Matcher
Rust 1.78 / rustc
1
2
3
4
5
6
7
8
9
10
11
12
13
Output Console
Click "Run Code" to view the rendered output.
How it works: The match expression exhaustively evaluates http_status and binds the descriptive text.

Your Turn: Micro Challenge

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

Micro Exercise

Match dice roll outcome

  • PointGiven let roll = 6;
  • PointUse match roll { 6 => "Critical Hit", 1 => "Critical Miss", _ => "Normal Roll" };
  • PointPrint "Outcome: Critical Hit".
1
2
3
4
5
6
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