Conceptcore
Pattern matching
Also called structural pattern matching, match expression, destructuring, case analysis, switch patterns.
A pattern describes the shape of a value. A match tests a value against a list of patterns, takes the first one that fits, and binds the parts of the value to names in one step.
- Applies to
- core-languages
Idea
Code often asks two questions about a value at the same time. What shape does it have? And what is inside it? A command is a list of words. If the first word is go and there is one more word, that word is the direction. Without patterns, the program first tests the length, then tests the first word, then reads the second word by its index. The test and the read are separate steps, and a mistake in one of them gives a wrong read.
A pattern puts the two steps together. It looks like the value that it matches, with names in the places of the parts. A match tries its patterns from top to bottom. The first pattern that fits the value wins, and its names are bound to the parts of the value.
fn run(command: &[&str]) -> String {
match command {
[] => "nothing to do".to_string(),
["go", direction] => format!("you go {direction}"),
["take", item] => format!("you take the {item}"),
["take", first, rest @ ..] => format!("you take the {first} and {} more", rest.len()),
[verb, ..] => format!("unknown command: {verb}"),
}
}
fn main() {
let commands: [&[&str]; 5] = [
&["go", "north"],
&["take", "lamp"],
&["take", "lamp", "key", "map"],
&["dance"],
&[],
];
for command in commands {
println!("{}", run(command));
}
}
you go north
you take the lamp
you take the lamp and 2 more
unknown command: dance
nothing to do
["go", direction] matches a slice of exactly two elements whose first element is "go". It binds the second element to direction. rest @ .. binds the remaining elements to rest.4 The arm ["take", item] comes before ["take", first, rest @ ..], so a command with one item takes the first of the two arms. The last arm matches each slice that is not empty, and the first arm matches the empty slice. The compiler checks that the arms cover every slice.
How it works
Two jobs in one step. A pattern tests a value and binds names. The test can look at the type of the value, at its length, at constants inside it, and at the parts of its parts. The names are bound only if the whole test succeeds. In Rust, the names are visible in the guard and in the body of the arm, and not after the match.2 In Python, the names stay bound after the match statement, and a failed match can leave some names bound.8
The kinds of patterns. Most languages with patterns have the same small set. Each kind of pattern can hold other patterns, so a pattern can describe a value at any depth.
| Kind | Matches | Rust | Python | C# | Java |
|---|---|---|---|---|---|
| literal | a value equal to a constant | 0, "go" |
0, "go" |
0, "go" |
none |
| wildcard | any value, binds nothing | _ |
_ |
_ |
_ in a record pattern |
| variable | any value, and binds it | x |
x |
var x |
int x |
| type | a value of a run-time type | none | int() |
string s |
String s |
| record or class | a value of a type, and its fields | Point { x, y } |
Point(x=0) |
Point(0, var y) |
Point(int x, int y) |
| tuple and sequence | a fixed number of elements, or a rest | (a, b), [a, ..] |
(a, b), [a, *rest] |
(a, b), [a, ..] |
none |
| mapping | a dictionary that has some keys | none | {"type": t} |
none | none |
| range or relation | a value in a range | 1..=9 |
none | >= 1 and <= 9 |
none |
| or | a value that one of the alternatives matches | A | B |
A | B |
A or B |
none |
| binding | the value of a sub-pattern, and binds it | x @ 1..=9 |
[1, 2] as p |
Point(> 0, _) p |
none |
The sources for each column are the Rust Reference, the Python Language Reference, the C# reference and the JLS.181411 A Java pattern is a type pattern or a record pattern, and a record pattern holds only patterns of these kinds and _.11 So Java has no constant inside a record pattern, and a test for zero goes in a guard (see the Java example).
The order of the arms. The arms are tried from top to bottom, and the first arm that matches is chosen.28 So the order of the arms is part of the meaning. A specific arm must come before a general arm that also matches its values. A Go switch has the same rule for its cases.22
Guards. A guard is a boolean condition after a pattern, written if in Rust and Python and when in C# and Java. It runs only after the pattern matches, and it can use the names that the pattern bound. If the guard is false, the match goes on to the next arm.38 A guard can test what a pattern cannot, such as x == y for two parts of the same value.
Refutable and irrefutable patterns. A pattern that can fail is refutable, such as Some(x) or (0, y). A pattern that matches every value of its type is irrefutable, such as x or (a, b) for a pair.1 Rust lets a let statement, a function parameter and a for loop take only irrefutable patterns, because the program has nothing to do when the value does not match.5 So let (x, y) = point; takes a pair apart. if let and let else take refutable patterns and give code for the failure. In Python, a case block whose pattern cannot fail must be the last one.9
Destructuring without a test. Some languages have the binding half of patterns but not the testing half. JavaScript destructuring binds names to the properties of an object, but a missing property gives undefined and no error. Destructuring null or undefined throws a TypeError.21 A C++ structured binding takes apart a pair, a tuple, an array or a struct, and the number of names must equal the number of elements. A wrong count is a compile error.24 Neither form can choose between several shapes.
What the compiler checks. A compiler that knows the patterns can find two mistakes. A match is non-exhaustive when some value matches no arm. An arm is useless when the arms above it already match each value that it matches.26
| Language | Non-exhaustive match | Useless arm |
|---|---|---|
| Rust | error E0004 | warning unreachable pattern |
Java, switch with patterns |
compile error | compile error, the label is dominated12 |
C#, switch expression |
warning, then SwitchExpressionException at run time16 |
error CS851015 |
| Python | nothing; the statement does nothing | SyntaxError only after an irrefutable pattern9 |
| Go, C, JavaScript | no patterns | no patterns |
The checks do not look inside guards. The compiler treats a guarded arm as an arm that may fail. The Rust book states that the compiler does not check exhaustiveness when match guards are involved.6 In Java, only unguarded case labels count when the compiler checks that a switch covers its type.13 The exhaustiveness check over the cases of a type is described in Sum types.
Compiling a match. The compiler turns the patterns into a sequence of simple tests. Luc Maranget describes two kinds of result. A decision tree never tests a part of the value more than once, but its code can grow very large. A backtracking automaton, first described by Lennart Augustsson in 1985, has code whose size grows linearly with the patterns, but it can test a part of the value again after it backtracks.27
In languages
| Language | Verdict | Note |
|---|---|---|
| Python | Native | |
| C++ | Absent | |
| Java | Native | |
| C | Absent | |
| C# | Native | |
| JavaScript | Idiom | |
| TypeScript | Idiom | |
| Go | Idiom | |
| Rust | Native | |
| Kotlin | Idiom | A when expression with a subject tests types with is and smart-casts the subject, a guard condition after if adds a test, and a when expression must cover all cases; when cannot take a value apart into nested parts; no example file because kotlinc is not installed on the machine that recorded the examples. Alternative: When with is checks and guard conditions, and a destructuring declaration such as val (x, y) = point for a data class. |
| SQL | Absent | SQL has no patterns that take a row or a value apart. Alternative: A CASE expression whose WHEN conditions are tested in order, and LIKE for string patterns. |
| Bash | Absent | |
| PowerShell | Absent | PowerShell has no patterns that take a value apart; switch converts each value to a string and runs the action of every condition that matches; no example file because pwsh is not installed on the machine that recorded the examples. Alternative: Switch with -Wildcard or -Regex on strings, with break in each action to stop after the first match, or scriptblock conditions such as { $_.X -eq 0 }. |
| HTML | Does not apply | Markup has no values to take apart. |
| CSS | Does not apply | Style rules have no values to take apart. |
| Regular expressions | Does not apply | A regular expression matches strings; it does not take apart structured values. |
The example files classify the same five points in each language: the origin, a point on an axis, a point on the diagonal, and any other point. The table gives the verdict for each tier 1 language.
| Language | Verdict | Form |
|---|---|---|
| Python | native | match with literal, capture, sequence, mapping, class, OR and AS patterns, and guards8 |
| JavaScript | idiom | destructuring and a chain of if tests; a match proposal is at Stage 12021 |
| TypeScript | idiom | as JavaScript, and a switch on a literal field that narrows a union |
| Java | native | type and record patterns in switch and instanceof, and guards after when11 |
| C | absent | switch on integer constants and a chain of if tests23 |
| C++ | absent | structured bindings and a chain of if tests; std::visit for a variant24 |
| C# | native | positional, property, relational, logical and list patterns in is and switch14 |
| Go | idiom | a switch with conditions; a type switch for interfaces22 |
| Rust | native | match, if let, let else, and patterns in let and parameters15 |
| Kotlin | idiom | when with is checks, smart casts and guard conditions18 |
| PowerShell | absent | switch with -Wildcard or -Regex on strings34 |
| Bash | absent | case with glob patterns on a string25 |
| SQL | absent | a CASE expression with conditions |
| Regex | n/a | a regular expression matches strings, not structured values |
| HTML/CSS | n/a | no values to take apart |
Python. The match statement has all the kinds of patterns in the table. A mapping pattern matches a dictionary that has the listed keys, and it ignores other keys.10 So it suits data from json.loads (see the Python example). The match statement describes each pattern in detail.
Java. A record pattern takes a record apart by its components, and its components can be record patterns again. There is no constant pattern inside a record pattern, so the Java example tests the zeros in guards.11
C#. A positional pattern calls the Deconstruct method of the type, and a tuple matches by position.14 Relational patterns such as >= 3 and < 6 test a range, and list patterns, from C# 11, take apart arrays and lists (see the C# example).1417
Kotlin. A when with a subject can test types with is, and the subject is then smart-cast in that branch. A guard condition, written is Animal.Cat if !animal.mouseHunter, adds a test to a branch. A when used as an expression must cover all cases.18 Guard conditions are stable from Kotlin 2.2.0.19 when cannot take a value apart into nested parts. Kotlin is not installed on the machine that recorded the examples, so it has no example file.
Go, C and C++. These languages have no patterns. A Go switch with no expression tests its case conditions from top to bottom, and the first true case runs.22 A C switch compares an integer with integer constants only.23 In all three, a chain of if tests does the work of the arms, and the programmer must keep the tests and the reads in step.
PowerShell. A switch statement converts its values to strings, and it can match them as wildcard patterns or as regular expressions. Unlike a match, it runs the action of each condition that matches, not only the first one. A break in an action stops the switch.34
Bash. A case statement matches one string against glob patterns. The patterns use the rules of filename expansion, and no match is tried after the first one.25 The Bash example joins the two numbers into one string, such as 3,0, so that *,0 can match it.
def describe(point):
match point:
case (0, 0):
return "origin"
case (x, 0):
return f"on the x axis at {x}"
case (0, y):
return f"on the y axis at {y}"
case (x, y) if x == y:
return f"on the diagonal at {x}"
case (x, y):
return f"at {x}, {y}"
for point in [(0, 0), (3, 0), (0, -2), (4, 4), (1, 5)]:
print(f"{point}: {describe(point)}")
(0, 0): origin
(3, 0): on the x axis at 3
(0, -2): on the y axis at -2
(4, 4): on the diagonal at 4
(1, 5): at 1, 5
A mapping pattern takes apart a dictionary, such as the result of json.loads. It matches when the listed keys are present, and it ignores other keys.
import json
def handle(event):
match event:
case {"type": "click", "pos": [x, y]}:
return f"click at {x}, {y}"
case {"type": "key", "key": str(key)} if len(key) == 1:
return f"key {key}"
case {"type": kind}:
return f"ignored {kind}"
case _:
return "not an event"
lines = [
'{"type": "click", "pos": [10, 20], "time": 5}',
'{"type": "key", "key": "q"}',
'{"type": "key", "key": "Enter"}',
'{"type": "scroll"}',
'[1, 2]',
]
for line in lines:
print(handle(json.loads(line)))
click at 10, 20
key q
ignored key
ignored scroll
not an event
- Verdict: native. The
matchstatement is in Python 3.10 and later. It has literal, capture, wildcard, sequence, mapping, class, OR and AS patterns, and a guard afterif. - The interpreter does not check that the cases are complete. If no case matches, the statement does nothing.
- A bare name, such as
x, always binds. To compare with a constant, use a dotted name, such asColor.RED. The match statement describes each kind of pattern.
#include <cstdio>
#include <string>
#include <utility>
std::string describe(std::pair<int, int> point) {
auto [x, y] = point;
if (x == 0 && y == 0) return "origin";
if (y == 0) return "on the x axis at " + std::to_string(x);
if (x == 0) return "on the y axis at " + std::to_string(y);
if (x == y) return "on the diagonal at " + std::to_string(x);
return "at " + std::to_string(x) + ", " + std::to_string(y);
}
int main() {
std::pair<int, int> points[] = {{0, 0}, {3, 0}, {0, -2}, {4, 4}, {1, 5}};
for (auto point : points) {
std::printf("(%d, %d): %s\n", point.first, point.second, describe(point).c_str());
}
}
(0, 0): origin
(3, 0): on the x axis at 3
(0, -2): on the y axis at -2
(4, 4): on the diagonal at 4
(1, 5): at 1, 5
- Verdict: absent. Standard C++ has no
match. A structured binding,auto [x, y] = point, takes a pair, a tuple, an array or a struct apart, and a chain ofiftests does the matching. - A structured binding must name each element. A count that does not match the type is a compile error.
std::visiton astd::variantselects a branch by the type of the value. Sum types shows it.
public class Main {
record Point(int x, int y) {}
static String describe(Point p) {
return switch (p) {
case Point(int x, int y) when x == 0 && y == 0 -> "origin";
case Point(int x, int y) when y == 0 -> "on the x axis at " + x;
case Point(int x, int y) when x == 0 -> "on the y axis at " + y;
case Point(int x, int y) when x == y -> "on the diagonal at " + x;
case Point(int x, int y) -> "at " + x + ", " + y;
};
}
public static void main(String[] args) {
Point[] points = {
new Point(0, 0), new Point(3, 0), new Point(0, -2), new Point(4, 4), new Point(1, 5)
};
for (Point p : points) {
System.out.println(p + ": " + describe(p));
}
Object o = "hello";
if (o instanceof String s && s.length() > 3) {
System.out.println("a long string: " + s);
}
}
}
Point[x=0, y=0]: origin
Point[x=3, y=0]: on the x axis at 3
Point[x=0, y=-2]: on the y axis at -2
Point[x=4, y=4]: on the diagonal at 4
Point[x=1, y=5]: at 1, 5
a long string: hello
- Verdict: native. Java 21 has type patterns and record patterns in
switchand ininstanceof, and a guard afterwhen. - A record pattern holds only type patterns, record patterns and
_. It cannot hold a constant, such asPoint(0, 0), so this example tests the zeros in guards. - The last case has no guard. Guarded cases do not count when the compiler checks that the
switchcovers its type, so without that case theswitchdoes not compile.
#include <stdio.h>
struct point { int x, y; };
static void describe(struct point p, char *out, size_t size) {
if (p.x == 0 && p.y == 0)
snprintf(out, size, "origin");
else if (p.y == 0)
snprintf(out, size, "on the x axis at %d", p.x);
else if (p.x == 0)
snprintf(out, size, "on the y axis at %d", p.y);
else if (p.x == p.y)
snprintf(out, size, "on the diagonal at %d", p.x);
else
snprintf(out, size, "at %d, %d", p.x, p.y);
}
int main(void) {
struct point points[] = {{0, 0}, {3, 0}, {0, -2}, {4, 4}, {1, 5}};
char text[64];
for (size_t i = 0; i < sizeof points / sizeof points[0]; i++) {
describe(points[i], text, sizeof text);
printf("(%d, %d): %s\n", points[i].x, points[i].y, text);
}
return 0;
}
(0, 0): origin
(3, 0): on the x axis at 3
(0, -2): on the y axis at -2
(4, 4): on the diagonal at 4
(1, 5): at 1, 5
- Verdict: absent. C has no patterns. A
switchcompares an integer with integer constants, and a chain ofiftests does the rest. - A
caselabel falls through to the next label unless abreakstops it.
(int X, int Y)[] points = { (0, 0), (3, 0), (0, -2), (4, 4), (1, 5) };
foreach (var point in points)
{
Console.WriteLine($"{point}: {Describe(point)}");
}
static string Describe((int X, int Y) point) => point switch
{
(0, 0) => "origin",
(var x, 0) => $"on the x axis at {x}",
(0, var y) => $"on the y axis at {y}",
var (x, y) when x == y => $"on the diagonal at {x}",
var (x, y) => $"at {x}, {y}",
};
(0, 0): origin
(3, 0): on the x axis at 3
(0, -2): on the y axis at -2
(4, 4): on the diagonal at 4
(1, 5): at 1, 5
List patterns, from C# 11, take apart an array or a list. A slice pattern .. matches the remaining elements, and .. var rest binds them.
string[][] commands =
{
new[] { "go", "north" },
new[] { "take", "lamp" },
new[] { "take", "lamp", "key", "map" },
new[] { "dance" },
new string[0],
};
foreach (var command in commands)
{
Console.WriteLine(Run(command));
}
static string Run(string[] command) => command switch
{
[] => "nothing to do",
["go", var direction] => $"you go {direction}",
["take", var item] => $"you take the {item}",
["take", var first, .. var rest] => $"you take the {first} and {rest.Length} more",
[var verb, ..] => $"unknown command: {verb}",
};
you go north
you take the lamp
you take the lamp and 2 more
unknown command: dance
nothing to do
- Verdict: native. C# has declaration, type, constant, relational, logical (
and,or,not), property, positional,var, discard and list patterns. They work inis, in aswitchstatement and in aswitchexpression. - A
switchexpression that can miss a value gives a warning, and it throwsSwitchExpressionExceptionat run time when no arm matches. - An arm that an earlier arm already covers is an error (CS8510).
function describe([x, y]) {
if (x === 0 && y === 0) return "origin";
if (y === 0) return `on the x axis at ${x}`;
if (x === 0) return `on the y axis at ${y}`;
if (x === y) return `on the diagonal at ${x}`;
return `at ${x}, ${y}`;
}
const points = [[0, 0], [3, 0], [0, -2], [4, 4], [1, 5]];
for (const point of points) {
console.log(`(${point.join(", ")}): ${describe(point)}`);
}
const { type, pos: [px, py] = [0, 0], size } = { type: "click", pos: [10, 20] };
console.log(type, px, py, size);
(0, 0): origin
(3, 0): on the x axis at 3
(0, -2): on the y axis at -2
(4, 4): on the diagonal at 4
(1, 5): at 1, 5
click 10 20 undefined
- Verdict: idiom. JavaScript has destructuring but no
match. Destructuring binds names, but it does not test the shape of the value. - A missing property gives
undefined, or the default value after=. Heresizeisundefined, and nothing reports it. - The TC39 pattern matching proposal is at Stage 1. It would add a
matchexpression and anisoperator.
type Point = readonly [number, number];
function describe([x, y]: Point): string {
if (x === 0 && y === 0) return "origin";
if (y === 0) return `on the x axis at ${x}`;
if (x === 0) return `on the y axis at ${y}`;
if (x === y) return `on the diagonal at ${x}`;
return `at ${x}, ${y}`;
}
const points: Point[] = [[0, 0], [3, 0], [0, -2], [4, 4], [1, 5]];
for (const point of points) {
console.log(`(${point.join(", ")}): ${describe(point)}`);
}
(0, 0): origin
(3, 0): on the x axis at 3
(0, -2): on the y axis at -2
(4, 4): on the diagonal at 4
(1, 5): at 1, 5
- Verdict: idiom. TypeScript has no
match. Destructuring in the parameter list takes the tuple apart, and a chain ofiftests does the matching. - For a union with a
kindfield, aswitchonkindnarrows the type in each branch. Sum types shows this form and its exhaustiveness check. - The TC39 pattern matching proposal, at Stage 1, would add a
matchexpression to JavaScript and so to TypeScript.
package main
import "fmt"
type Point struct{ X, Y int }
func describe(p Point) string {
switch {
case p.X == 0 && p.Y == 0:
return "origin"
case p.Y == 0:
return fmt.Sprintf("on the x axis at %d", p.X)
case p.X == 0:
return fmt.Sprintf("on the y axis at %d", p.Y)
case p.X == p.Y:
return fmt.Sprintf("on the diagonal at %d", p.X)
default:
return fmt.Sprintf("at %d, %d", p.X, p.Y)
}
}
func main() {
points := []Point{{0, 0}, {3, 0}, {0, -2}, {4, 4}, {1, 5}}
for _, p := range points {
fmt.Printf("%v: %s\n", p, describe(p))
}
switch p := (Point{0, 0}); p {
case Point{0, 0}:
fmt.Println("a struct compares with ==")
}
}
{0 0}: origin
{3 0}: on the x axis at 3
{0 -2}: on the y axis at -2
{4 4}: on the diagonal at 4
{1 5}: at 1, 5
a struct compares with ==
- Verdict: idiom. Go has no patterns. A
switchwith no expression tests eachcasecondition from top to bottom, and the first true one runs. - A
switchon a value compares the value with each case with==, so a comparable struct can be a case. Nothing binds the fields. - A type switch selects a branch by the dynamic type of an interface value. Sum types shows it.
fn describe(point: (i32, i32)) -> String {
match point {
(0, 0) => "origin".to_string(),
(x, 0) => format!("on the x axis at {x}"),
(0, y) => format!("on the y axis at {y}"),
(x, y) if x == y => format!("on the diagonal at {x}"),
(x, y) => format!("at {x}, {y}"),
}
}
fn main() {
for point in [(0, 0), (3, 0), (0, -2), (4, 4), (1, 5)] {
println!("{:?}: {}", point, describe(point));
}
let (x, y) = (7, 8);
println!("let takes apart a tuple: {x} {y}");
let input = "42";
let Ok(n) = input.parse::<i32>() else {
println!("not a number");
return;
};
println!("let else bound {n}");
}
(0, 0): origin
(3, 0): on the x axis at 3
(0, -2): on the y axis at -2
(4, 4): on the diagonal at 4
(1, 5): at 1, 5
let takes apart a tuple: 7 8
let else bound 42
- Verdict: native.
match,if let,while let,let,let elseand function parameters all take patterns. - A
matchmust be exhaustive, or it does not compile (error E0004). The compiler does not use the guards when it checks this, so the last arm here has no guard. letand function parameters accept only irrefutable patterns.let elseaccepts a refutable pattern, and itselseblock must leave the scope, here withreturn.
describe() {
case "$1,$2" in
0,0) echo "origin" ;;
*,0) echo "on the x axis at $1" ;;
0,*) echo "on the y axis at $2" ;;
*)
if [ "$1" = "$2" ]; then
echo "on the diagonal at $1"
else
echo "at $1, $2"
fi
;;
esac
}
for point in "0 0" "3 0" "0 -2" "4 4" "1 5"; do
set -- $point
echo "($1, $2): $(describe "$1" "$2")"
done
(0, 0): origin
(3, 0): on the x axis at 3
(0, -2): on the y axis at -2
(4, 4): on the diagonal at 4
(1, 5): at 1, 5
- Verdict: absent. Bash has no structured values to take apart. A
casestatement matches one string against glob patterns, such as*,0, and the first match wins. - A pattern has no guard, so the diagonal test is an
ifinside the last branch. - A glob pattern is not a regular expression.
*matches any string, and?matches one character.
Variations and related ideas
- Sum types. Patterns are the usual way to use a sum type, because a constructor pattern tests the case and binds its fields in one step. Sum types covers the exhaustiveness check over the cases of a type.
- Visitors. In a language with no patterns, the Visitor pattern gives one method for each class of node. A
matchwith one arm for each case does the same job in one function. Visitor pattern compares the two. - Option and Result.
if let Some(x)andmatchonOkandErrare the normal way to read an optional value or a result in Rust. Null reference and Error value describe these types. - String patterns. Glob patterns, such as
*.txt, and regular expressions match strings, not structured values. They share the name but not the idea. Acasein Bash andswitch -Wildcardin PowerShell use glob patterns.2534 - Interpreters. A tree-walking interpreter is the classic use of nested patterns, with one arm for each kind of node. Tree-walking interpreter shows one.
Pitfalls
A name that binds instead of compares
In a pattern, a plain name is a variable that matches anything. It is a comparison only if the name refers to a constant. Rust gives path patterns, such as a constant, precedence over identifier patterns.4 So a misspelled constant becomes a new variable that matches each value.
This code runs but gives a wrong answer on purpose.
const ORIGIN: (i32, i32) = (0, 0);
fn describe(point: (i32, i32)) -> &'static str {
match point {
ORIGN => "origin",
_ => "elsewhere",
}
}
fn main() {
println!("{}", describe((0, 0)));
println!("{}", describe((3, 4)));
}
origin
origin
(3, 4) is not the origin. rustc warns that the _ arm is unreachable and that the variable ORIGN is unused, but the program builds. The fix is the correct name, ORIGIN. Python has the same trap in a stronger form, because a bare name in a pattern always binds, even when a constant has that name. The match statement shows it. The same rule makes an inner name shadow an outer variable. The Rust book shows Some(y) binding a new y in place of a comparison with the outer y.7 Use a guard, such as Some(n) if n == y, to compare with a variable.
The order of the arms
The first arm that matches wins. Here the diagonal arm comes before the origin arm. The origin is also on the diagonal, so it takes the diagonal arm.
This code runs but gives a wrong answer on purpose.
fn describe(point: (i32, i32)) -> String {
match point {
(x, y) if x == y => format!("on the diagonal at {x}"),
(0, 0) => "origin".to_string(),
(x, y) => format!("at {x}, {y}"),
}
}
fn main() {
println!("{}", describe((0, 0)));
println!("{}", describe((2, 2)));
}
on the diagonal at 0
on the diagonal at 2
The compiler does not warn, because the first arm has a guard, and the compiler does not know what a guard does. The fix is to put the specific arm, (0, 0), first. When no guard is involved, the compiler can see the mistake. In Java, a case label that an earlier label covers is a compile error.12
This code does not compile, on purpose.
public class Main {
static String describe(Object o) {
return switch (o) {
case CharSequence cs -> "text of length " + cs.length();
case String s -> "a string";
default -> "something else";
};
}
public static void main(String[] args) {
System.out.println(describe("hello"));
}
}
javac reports this case label is dominated by a preceding case label. A String is a CharSequence, so the second case can never run. C# reports error CS8510 for the same mistake, and Rust gives the warning unreachable pattern.15
A guard that the exhaustiveness check cannot see
These three arms cover every integer. But the compiler does not reason about guards, so it must assume that both guarded arms can fail.6
This code does not compile, on purpose.
fn sign(n: i32) -> &'static str {
match n {
n if n < 0 => "negative",
0 => "zero",
n if n > 0 => "positive",
}
}
fn main() {
println!("{}", sign(-5));
}
rustc stops with error[E0004]: non-exhaustive patterns: `i32::MIN..=-1_i32` and `1_i32..=i32::MAX` not covered. One fix is range patterns, which the compiler can check: i32::MIN..=-1, 0 and 1..=i32::MAX. Another fix is a last arm without a guard, _ => "positive". A guard is a test the compiler cannot read, so prefer a pattern where a pattern can say the same thing. Java has the same rule: a guarded case label does not count when the compiler checks that the switch is exhaustive.13
Destructuring that does not test
JavaScript destructuring binds names, but it does not check that the properties exist. A missing property gives undefined, and the error appears later, far from its cause.21
This code runs but gives a wrong answer on purpose.
function area({ width, height }) {
return width * height;
}
console.log(area({ width: 2, height: 3 }));
console.log(area({ width: 2, heigth: 3 }));
6
NaN
The misspelled heigth gives undefined for height, and the product is NaN. A value of null fails at once, because destructuring null throws a TypeError.21
This code raises TypeError, on purpose.
function area({ width, height }) {
return width * height;
}
console.log(area(null));
The fix is an explicit test of the shape before the use, or TypeScript, which reports the misspelled property at compile time. A real match would choose a branch only when the shape fits.
Cost
- The tests. A match does the tests that a hand-written chain of
ifstatements would do. A compiler can order and share the tests. A decision tree tests each part of the value at most once, but its code can become large. A backtracking automaton keeps the code small, but it can test a part again.27 - Guards run code. A guard is an ordinary expression, and it runs each time its pattern matches. In Rust, a guard on an arm with an or-pattern can run once for each alternative.3 Keep guards cheap and free of side effects.
- Calls for some patterns. A C# positional pattern calls the
Deconstructmethod of the type.14 So the cost of such a pattern includes the cost of that method. - The checks are free at run time. The exhaustiveness and usefulness checks run in the compiler.26
History
- 1980. R. M. Burstall, D. B. MacQueen and D. T. Sannella described HOPE. A
datadeclaration listed the constructors of a type, and the designers asked that a compiler check whether a case analysis over the constructors is exhaustive.28 - 1985. Lennart Augustsson described the compilation of pattern matching to backtracking automata.27
- 2007. Luc Maranget defined two anomalies of ML pattern matching, useless clauses and non-exhaustive matches, and gave an algorithm that finds them. He integrated it in the Objective Caml compiler, and he showed that it also works for a non-strict language such as Haskell.26
- 2008. Maranget showed new heuristics for the compilation of patterns to decision trees, and he compared the result with optimized backtracking automata.27
- 2017 to 2022. C# 7.0, released in March 2017, added pattern matching. C# 8.0, in September 2019, added switch expressions and property, tuple and positional patterns. C# 9, in November 2020, added relational and logical patterns, and C# 11, in November 2022, added list patterns.17
- 2021. Python 3.10, released on 4 October 2021, added the
matchstatement (PEP 634).2930 - 2023. JDK 21, released on 19 September 2023, added record patterns (JEP 440) and pattern matching for
switch(JEP 441).313233 - 2025. Kotlin 2.2.0 made guard conditions in
whenstable.19 The TC39 proposal to add pattern matching to JavaScript is at Stage 1.20
References
- The Rust Reference, Patterns, Refutability, and the kinds of patterns (literal, identifier, wildcard, rest, range, struct, tuple struct, tuple, slice, path and or-patterns)
- The Rust Reference, Match expressions, rules expr.match.scrutinee-value (the first arm with a matching pattern is chosen), expr.match.pattern-vars and expr.match.guard.next
- The Rust Reference, Match expressions, rule expr.match.guard.behavior, and the note on a guard that runs once for each alternative of an or-pattern
- The Rust Reference, Patterns, rules patterns.ident.precedent (path patterns take precedence over identifier patterns) and patterns.ident.scrutinized (variable @ subpattern)
- The Rust Programming Language, 19.2 Refutability, Refutability, Whether a Pattern Might Fail to Match (let statements, function parameters and for loops take only irrefutable patterns)
- The Rust Programming Language, 19.3 Pattern Syntax, Adding Conditionals with Match Guards (the compiler does not check exhaustiveness when match guards are involved)
- The Rust Programming Language, 19.3 Pattern Syntax, Matching Named Variables, Listing 19-11 (a variable in a pattern shadows an outer variable of the same name)
- The Python Language Reference, Python 3.15, 8.6.1 Overview (the case blocks are tried in order, the guard runs after a successful match, and bindings outlive the block)
- The Python Language Reference, Python 3.15, 8.6.3 Irrefutable Case Blocks, and 8.6.4.4 Capture Patterns
- The Python Language Reference, Python 3.15, 8.6.4.9 Mapping Patterns
- The Java Language Specification, Java SE 25 Edition, 14.30.1 Kinds of Patterns (type patterns and record patterns, and the match-all pattern _ inside a record pattern)
- The Java Language Specification, Java SE 25 Edition, 14.11.1 Switch Blocks (a dominated switch label is a compile-time error; guarded and unguarded case labels)
- The Java Language Specification, Java SE 25 Edition, 14.11.1.1 Exhaustive Switch Blocks (only unguarded case labels count)
- C# language reference, Patterns, the list of patterns, Positional pattern (the generated code calls the Deconstruct method), and List patterns
- C# language reference, Patterns, Closed hierarchy patterns (error CS8510 for an arm that an earlier arm already handles, as in any class hierarchy)
- C# language reference, the switch expression, Non-exhaustive switch expressions (a compiler warning, and SwitchExpressionException at run time)
- The history of C#, C# version 7.0 (March 2017), 8.0 (September 2019), 9 (November 2020) and 11 (November 2022), the pattern matching items
- Kotlin documentation, Conditions and loops, When expressions and statements (a when expression must cover all cases; is checks with smart casts; guard conditions)
- What's new in Kotlin 2.2.0, Stable features: Guard conditions, non-local break and continue, and multi-dollar interpolation
- TC39 proposal, Pattern Matching, the Stage line (Stage 1), the match expression and the is operator
- MDN Web Docs, Destructuring, Default value, and Destructuring primitive values (destructuring null or undefined throws a TypeError)
- The Go Programming Language Specification, Expression switches (case expressions are evaluated left to right and top to bottom; a missing switch expression means true)
- N1570, ISO/IEC 9899:201x Committee Draft (C11), 6.8.4.2 paragraphs 1 and 3 (the controlling expression has integer type, and each case label is an integer constant expression)
- N4950, Working Draft, Programming Languages C++ (C++23), [dcl.struct.bind] paragraphs 3, 4 and 5 (the number of names must equal the number of elements)
- Bash Reference Manual, Conditional Constructs, case (patterns use the rules of filename expansion, and no match is tried after the first one)
- Luc Maranget, Warnings for pattern matching, Journal of Functional Programming 17(3), 2007, pp. 387-421, Abstract (useless clauses and non-exhaustive matches, and an algorithm in the Objective Caml compiler)
- Luc Maranget, Compiling pattern matching to good decision trees, ML 2008, pp. 35-46, 1 Introduction (decision trees and backtracking automata, and Augustsson 1985)
- R. M. Burstall, D. B. MacQueen and D. T. Sannella, HOPE, An experimental applicative language, LFP 1980, 2 Desirable programming language features, Exhaustive case analysis (pp. 136 and 137)
- PEP 634: Structural Pattern Matching: Specification, the header (Python-Version 3.10)
- PEP 619: Python 3.10 Release Schedule, Release Schedule (3.10.0 final on Monday, 2021-10-04)
- JEP 440: Record Patterns, the header (Release 21)
- JEP 441: Pattern Matching for switch, the header (Release 21)
- JDK 21, the schedule (General Availability on 19 September 2023)
- PowerShell documentation, about_Switch, Syntax (the Wildcard and Regex parameters), and Use break and continue to control the flow (the action of each matching condition runs)
About this page
The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.
| Ladder | Level | What the next level needs |
|---|---|---|
| Human proof | H0 no proof |
|
| Lean | L0 no Lean part |
|
| Visual | V0 none |
|
| Writing | W4 complete |
|
Tag M6NX0X Edit this page History Report an error Markdown