Conceptcore
Match guards
Also called guard, guards, case guard, pattern guard, guard condition, guard clause in a match, when clause, at binding, as pattern.
A match guard is a boolean condition on a match arm. The arm is chosen only when its pattern matches and its guard is true; otherwise the match tries the next arm.
- Applies to
- core-languages
Idea
A pattern can test the shape of a value and constants inside it. It cannot compare two parts of the value, or call a function. A match guard adds such a test. It is a boolean condition after the pattern of a match arm, and the arm is chosen only when the pattern matches and the condition is true. The Python tutorial states: "We can add an if clause to a pattern, known as a “guard”. If the guard is false, match goes on to try the next case block"1.
In this Python function, the second case needs a guard, because no pattern can test that a divides by b:
def divide(cmd):
match cmd:
case ("div", _, 0):
return "error: divide by zero"
case ("div", a, b) if a % b == 0:
return a // b
case ("div", a, b):
return a / b
case ("add", a, b):
return a + b
for cmd in [("div", 6, 0), ("div", 6, 3), ("div", 7, 2), ("add", 1, 2)]:
print(divide(cmd))
error: divide by zero
2
3.5
3
The command ("div", 7, 2) matches the pattern of the second case, but its guard is false, so the third case takes it.
How it works
- First the pattern, then the guard. The guard runs only after its pattern matches, and it can use the names that the pattern bound. The Rust Book defines a guard as "an additional if condition, specified after the pattern in a match arm, that must also match for that arm to be chosen"2. The Rust Reference states: "When the pattern matches successfully, the pattern guard is executed"3. In Python, "value capture happens before the guard is evaluated"1.
- A false guard goes on to the next arm. A false guard does not end the match. The arm is skipped, and the arms after it are tried in order. PEP 634 states that "guard evaluation must happen in order", and "Guard evaluation must stop once a case block is selected"4.
- One guard for each arm. An arm has at most one guard, but the guard can be any boolean expression. To combine conditions, use
and,&&or||in the guard. - A guard on an or-pattern. When an arm has several patterns joined with
|, the guard applies to each of them. The Rust Book states that "the match guard condition will apply to all the patterns"2. - A guard is not a pattern. A name in a pattern binds a new variable. A name in a guard reads a variable. The Rust Book explains this for the guard
if n == y: "The match guard if n == y is not a pattern and therefore doesn’t introduce new variables"2. So a guard is the way to compare a part of the value with a variable from outside the match. - Guards and exhaustiveness. A compiler that checks that a match covers each value does not look inside guards. The Rust Book states that "the compiler doesn’t try to check for exhaustiveness when match guard expressions are involved"2. Kotlin gives the same rule: "If you use guard conditions in when expressions without an else branch, the compiler requires you to handle every possible case to avoid runtime errors"5. So the last arm of a match is usually an arm without a guard.
In languages
| Language | Verdict | Note |
|---|---|---|
| Python | Native | |
| C++ | Absent | |
| Java | Native | |
| C | Absent | |
| C# | Native | |
| JavaScript | Absent | |
| TypeScript | Absent | |
| Go | Idiom | |
| Rust | Native | |
| Kotlin | Native | A branch of a when expression with a subject can have a guard condition after `if`, such as `is Animal.Cat if !animal.mouseHunter`. Kotlin 2.2.0 made guard conditions stable. No example file exists because kotlinc is not installed. |
| SQL | Idiom | A searched CASE expression has a boolean condition in each WHEN clause, and gives the result of the first WHEN that is true. There are no patterns, so each condition does the work of a guard. No example file exists. |
| Bash | Absent | |
| PowerShell | Idiom | A switch condition can be a scriptblock that returns a boolean value, such as `{ $_ -lt 0 }`, and the action runs when the scriptblock is true. There are no patterns, so the scriptblock does the work of a guard. No example file exists because pwsh is not installed. |
| HTML | Does not apply | HTML has no match statement. |
| CSS | Does not apply | CSS has no match statement. |
| Regular expressions | Does not apply | A regular expression has no match statement. |
| Language | Guard | Example |
|---|---|---|
| Python | if after the pattern of a case6 |
case (a, b) if a == b: |
| Rust | if after the pattern of an arm3 |
(a, b) if a == b => |
| C# | when after the pattern of a switch expression arm7 |
var (a, b) when a == b => |
| Java | when after the pattern of a case label8 |
case Point(int a, int b) when a == b -> |
| Kotlin | if after the condition of a when branch with a subject5 |
is Animal.Cat if !animal.mouseHunter -> |
| Go | none; a switch with no expression tests conditions9 | case x == y: |
| JavaScript, TypeScript | none; no match statement10 |
an if chain |
| C, C++ | none; a case takes only a constant | an if chain |
| Bash | none; a case pattern is a glob11 |
an if in the clause |
- Python. A guard takes "the form: if followed by an expression"6, after the patterns of a
case. "If the pattern succeeds, the corresponding guard (if present) is evaluated"6. If the guard is true, the case block is selected. - Rust. "Pattern guards appear after the pattern following the if keyword and consist of an Expression with a boolean type or a conditional let match"3. So a guard can also match a second pattern with
let. - C#. "A case guard is another condition that must be satisfied together with a matched pattern"7. C# also has relational patterns, which "compare an expression result with a specified constant"12. So a pattern such as
< 0does a test that needs a guard in Python or Java. - Java. JEP 441 allows "when clauses in switch blocks to specify guards to pattern case labels"8. "Only pattern labels can have guards"8, so
case 0 when ...is not valid. JEP 441 is standard in Java 218. - Kotlin. "Guard conditions allow you to include more than one condition to the branches of a when expression or statement"5. "The code in a branch with a guard condition runs only if both the primary condition and the guard condition evaluate to true"5. Kotlin 2.2.0 made guard conditions stable13.
- Go, JavaScript, TypeScript, C, C++ and Bash. These languages have no guards. Go has a switch with no expression, which is "equivalent to the boolean value true"9, so each case is a condition. A JavaScript proposal for pattern matching exists, but it is at stage 110.
def describe(n):
match n:
case 0:
return "zero"
case k if k < 0:
return "negative"
case k if k < 10 and k % 2 == 0:
return "small even"
case k if k < 10:
return "small odd"
case _:
return "large"
print(", ".join(describe(n) for n in [-5, 0, 4, 7, 12]))
negative, zero, small even, small odd, large
- Verdict: native. A
casecan have anifclause after its pattern. The capture patternkmatches each value and binds it, and then the guard decides. If the guard is false, thematchtries the next case.
#include <iostream>
#include <string_view>
static std::string_view describe(int n) {
if (n == 0) {
return "zero";
}
if (n < 0) {
return "negative";
}
if (n < 10 && n % 2 == 0) {
return "small even";
}
if (n < 10) {
return "small odd";
}
return "large";
}
int main() {
const char *sep = "";
for (int n : {-5, 0, 4, 7, 12}) {
std::cout << sep << describe(n);
sep = ", ";
}
std::cout << "\n";
}
negative, zero, small even, small odd, large
- Verdict: absent. C++ has no pattern matching. A
switchcase takes only a constant expression, so a chain ofifstatements tests the conditions in order.
public class Main {
static String describe(Integer n) {
return switch (n) {
case Integer k when k == 0 -> "zero";
case Integer k when k < 0 -> "negative";
case Integer k when k < 10 && k % 2 == 0 -> "small even";
case Integer k when k < 10 -> "small odd";
case Integer k -> "large";
};
}
public static void main(String[] args) {
StringBuilder out = new StringBuilder();
for (int n : new int[] {-5, 0, 4, 7, 12}) {
if (out.length() > 0) {
out.append(", ");
}
out.append(describe(n));
}
System.out.println(out);
}
}
negative, zero, small even, small odd, large
- Verdict: native. A pattern case label can have a guard after
when. The selector is anInteger, so each label is the type patternInteger kwith a guard. The last label has no guard, so the switch is exhaustive.
#include <stdio.h>
static const char *describe(int n) {
if (n == 0) {
return "zero";
}
if (n < 0) {
return "negative";
}
if (n < 10 && n % 2 == 0) {
return "small even";
}
if (n < 10) {
return "small odd";
}
return "large";
}
int main(void) {
const int values[] = {-5, 0, 4, 7, 12};
for (int i = 0; i < 5; i++) {
printf("%s%s", i > 0 ? ", " : "", describe(values[i]));
}
printf("\n");
return 0;
}
negative, zero, small even, small odd, large
- Verdict: absent. C has no pattern matching. A
switchcase takes only a constant, so a condition such asn < 0cannot be a case. A chain ofifstatements tests the conditions in order.
using System;
static string Describe(int n) => n switch
{
0 => "zero",
< 0 => "negative",
var k when k < 10 && k % 2 == 0 => "small even",
< 10 => "small odd",
_ => "large",
};
Console.WriteLine(string.Join(", ", Array.ConvertAll(new[] { -5, 0, 4, 7, 12 }, Describe)));
negative, zero, small even, small odd, large
- Verdict: native. A switch expression arm can have a case guard after the
whenkeyword. Relational patterns such as< 0and< 10need no guard. The arm for small even numbers needs one, because no pattern testsk % 2.
function describe(n) {
if (n === 0) {
return "zero";
}
if (n < 0) {
return "negative";
}
if (n < 10 && n % 2 === 0) {
return "small even";
}
if (n < 10) {
return "small odd";
}
return "large";
}
console.log([-5, 0, 4, 7, 12].map(describe).join(", "));
negative, zero, small even, small odd, large
- Verdict: absent. JavaScript has no
matchstatement, so it has no guards. A chain ofifstatements tests the conditions in order. A TC39 proposal to add pattern matching is at stage 1.
function describe(n: number): string {
if (n === 0) {
return "zero";
}
if (n < 0) {
return "negative";
}
if (n < 10 && n % 2 === 0) {
return "small even";
}
if (n < 10) {
return "small odd";
}
return "large";
}
console.log([-5, 0, 4, 7, 12].map(describe).join(", "));
negative, zero, small even, small odd, large
- Verdict: absent. TypeScript adds no pattern matching to JavaScript, so it has no guards. A chain of
ifstatements tests the conditions in order.
package main
import (
"fmt"
"strings"
)
func describe(n int) string {
switch {
case n == 0:
return "zero"
case n < 0:
return "negative"
case n < 10 && n%2 == 0:
return "small even"
case n < 10:
return "small odd"
default:
return "large"
}
}
func main() {
var words []string
for _, n := range []int{-5, 0, 4, 7, 12} {
words = append(words, describe(n))
}
fmt.Println(strings.Join(words, ", "))
}
negative, zero, small even, small odd, large
- Verdict: idiom. Go has no patterns, so it has no guards. A switch with no expression tests each case as a boolean condition, in order, and each condition does the work of a guard.
fn describe(n: i32) -> &'static str {
match n {
0 => "zero",
k if k < 0 => "negative",
k if k < 10 && k % 2 == 0 => "small even",
k if k < 10 => "small odd",
_ => "large",
}
}
fn main() {
let words: Vec<&str> = [-5, 0, 4, 7, 12].iter().map(|&n| describe(n)).collect();
println!("{}", words.join(", "));
}
negative, zero, small even, small odd, large
- Verdict: native. A match arm can have
ifand a condition after its pattern. The compiler does not count a guarded arm when it checks that the match is exhaustive, so the last arm_has no guard.
describe() {
local n=$1
case $n in
0) echo "zero" ;;
-*) echo "negative" ;;
*)
if ((n < 10 && n % 2 == 0)); then
echo "small even"
elif ((n < 10)); then
echo "small odd"
else
echo "large"
fi
;;
esac
}
result=""
for n in -5 0 4 7 12; do
result+="${result:+, }$(describe "$n")"
done
echo "$result"
negative, zero, small even, small odd, large
- Verdict: absent. A
casepattern is a glob, and a clause has no condition after its pattern. The globs0and-*take zero and the negative numbers. The last clause tests the other conditions with anif.
Variations and related ideas
- Guards outside a match. Wikipedia gives a wider meaning: "a guard is a Boolean expression that must evaluate to true if the execution of the program is to continue in the branch in question"14. The guard clause at the start of a function, which returns early for a bad argument, uses the same word14.
- Several guards in order. In Haskell, a function equation can have a list of guards. "If there are several parallel guards, they are normally tried in a top-to-bottom order, and the branch of the first to pass is chosen"14. This is the same rule as for the arms of a match.
- Pattern guards. A pattern guard is a guard that is itself a pattern match. "In effect, a match of the pattern is taken to mean pass"14. A Rust guard can be "a conditional let match"3, which is a guard of this kind.
- Binding a tested value. A guard is not the only way to test a value and keep it. With a range pattern, Rust can test and bind in the pattern: "The at operator @ lets us create a variable that holds a value at the same time we’re testing that value for a pattern match"2. So
id @ 3..=7replacesid if id >= 3 && id <= 7. Python has the AS pattern: "An AS pattern matches an OR pattern on the left of the as keyword against a subject"6, socase 1 | 2 | 3 as n:binds the value that matched. A pattern of this kind counts in the exhaustiveness check, but a guard does not2. - Dominance. Java checks the order of the case labels. "It is a compile-time error if any switch label in a switch block is dominated"15. "An unguarded pattern case label dominates a guarded pattern case label that has the same pattern"8. So a label with a guard must come before the same label without a guard.
Pitfalls
A name that shadows in place of a guard
A name in a pattern binds a new variable, even if a variable with that name exists outside the match. The Rust Book shows this with the pattern Some(y): "Because each of these kinds of expressions starts a new scope, variables declared as part of a pattern inside these expressions will shadow those with the same name outside the constructs"2. Here the second arm was meant to match only 10:
This code runs but gives a wrong answer on purpose.
fn main() {
let y = 10;
for x in [Some(5), Some(10)] {
match x {
Some(50) => println!("got 50"),
Some(y) => println!("matched, y = {y}"),
None => println!("none"),
}
}
}
matched, y = 5
matched, y = 10
The arm Some(y) matches each Some, and the outer y is not used. The fix is a new name and a guard: Some(n) if n == y. Then only Some(10) takes the arm, because "This y is the outer y rather than a new y shadowing it"2.
A guard that the exhaustiveness check cannot see
The compiler treats a guarded arm as an arm that can fail. These two guards cover each Integer, but the switch does not compile, because the compiler does not look inside the guards:
This code does not compile, on purpose.
public class Main {
static String sign(Integer n) {
return switch (n) {
case Integer k when k < 0 -> "negative";
case Integer k when k >= 0 -> "not negative";
};
}
public static void main(String[] args) {
System.out.println(sign(-1));
}
}
The fix is a last label without a guard: case Integer k with no when. Rust gives the same error for a match with guards on each arm2.
A guarded label after the same label without a guard
The arms are tried in order, so an arm without a guard takes each value that a later arm with the same pattern would take. In Python and Rust, the later arm never runs. In Java, the order is a compile-time error, because the first label dominates the second158:
This code does not compile, on purpose.
public class Main {
static String size(String s) {
return switch (s) {
case String t -> "text";
case String t when t.isEmpty() -> "empty";
};
}
public static void main(String[] args) {
System.out.println(size(""));
}
}
Put the label with the guard first.
A guard with side effects
A guard is an ordinary expression, so it can change state. Python allows this: "Guards are allowed to have side effects as they are expressions"6. But a guard can run more often than it seems. In Rust, an arm with an or-pattern runs its guard once for each alternative that matches: "Multiple matches using the | operator can cause the pattern guard and the side effects it has to execute multiple times"3. In this program, the value 1 matches both alternatives, so the guard runs twice:
use std::cell::Cell;
fn main() {
let calls = Cell::new(0);
match 1 {
1 | _ if {
calls.set(calls.get() + 1);
false
} => println!("first arm"),
_ => println!("second arm"),
}
println!("the guard ran {} times", calls.get());
}
second arm
the guard ran 2 times
Keep guards free of side effects.
Cost
- A guard is a test at run time. A guard runs each time its pattern matches, and it costs what its expression costs. A guard that calls a slow function makes each match slow.
- Guards limit the compiler. A compiler can merge and reorder the tests of patterns, but it must run guards in order4, because a guard can have side effects6. So a match with many guards is close to an
ifchain. - Repeated runs. In Rust, a guard on an or-pattern can run once for each alternative3.
History
- 1963 to 1966. "A simple conditional expression, already present in CPL in 1963, has a guard on first sub-expression"14. In 1966, ISWIM had a conditional expression with no required last case, "thus separating guard from the concept of choosing either-or"14.
- KRC. Wikipedia names KRC, a small version of SASL, as "one of the first programming languages to use the term" guard14. A function definition had several clauses, and a guard after each clause chose the clause to apply14.
- 1997. The idea of a pattern guard "was introduced in a proposal for Haskell by Simon Peyton Jones titled A new view of guards in April 1997"14.
- 2020. PEP 634, created on 12 September 2020, specified the Python
matchstatement with guards for Python 3.104. - 2023. JEP 441 added guards with
whento Java 218. - 2025. Kotlin 2.2.0, released on 23 June 2025, made guard conditions in
whenstable13.
References
- More Control Flow Tools, The Python Tutorial, 4.7 match Statements
- Pattern Syntax, The Rust Programming Language, Adding Conditionals with Match Guards; Matching Named Variables; Using @ Bindings
- The Rust Reference, Match expressions, Match guards
- PEP 634: Structural Pattern Matching: Specification, Guards; the header (Python-Version 3.10)
- Conditions and loops, Kotlin documentation, Guard conditions in when expressions
- Compound statements, The Python Language Reference, 8.6.1 Overview; 8.6.2 Guards; 8.6.4.2 AS Patterns
- switch expression, C# language reference, Case guards
- JEP 441: Pattern Matching for switch, Case refinement; Dominance of case labels; the header (Release 21)
- The Go Programming Language Specification, Expression switches
- ECMAScript Pattern Matching, TC39 proposal, README, Status
- Conditional Constructs, Bash Reference Manual, case
- Pattern matching, C# language reference, Relational patterns
- What's new in Kotlin 2.2.0, the header (Released June 23, 2025); Language
- Guard (computer science), Wikipedia, Terminology; Multiple guards; Evolution; Pattern guard
- The Java Language Specification, Java SE 25 Edition, 14.11.1 Switch Blocks
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 ZZRHD3 Edit this page History Report an error Markdown