Sub topic 7
Literal, range, wildcard, and or-patterns
This chapter assumes you know basic Rust syntax, how match branches on a value, and that a match must cover every case. Here you will learn the four simple pattern kinds that match arms use to test values.
Literal patterns
A literal pattern matches one exact value. Integers, characters, booleans, and string slices all work as literals.
fn describe(n: i32) -> &'static str {
match n {
0 => "zero",
1 => "one",
_ => "many",
}
}
fn main() {
println!("{}", describe(1));
}Arms are tested from top to bottom. The first arm that matches wins.
Range patterns
A range pattern matches every value in an inclusive range, written start..=end. Ranges work on integers and char. The compiler needs to know the bounds at compile time, so you cannot use variables in them.
An open-ended range such as 10.. matches everything from 10 upward. A range with no start, ..=9, matches everything up to and including 9. The exclusive form a..b is also allowed in patterns in current stable Rust.
Wildcard and or-patterns
The wildcard _ matches any value and does not bind it. It usually goes in the last arm, so the match covers every remaining case. If you want to use the value, give the catch-all a name instead, such as other => ....
The or-pattern | lets one arm match several alternatives. It can combine literals, ranges, and other patterns.
All four together
This example uses every kind in one match:
fn classify(c: char) -> &'static str {
match c {
'a' | 'e' | 'i' | 'o' | 'u' => "vowel",
'a'..='z' => "consonant",
'A'..='Z' => "uppercase letter",
'0'..='9' => "digit",
' ' | '\t' | '\n' => "whitespace",
_ => "other",
}
}
fn main() {
for c in ['e', 'x', 'Q', '7', ' ', '!'] {
println!("{c:?}: {}", classify(c));
}
}The vowel arm comes before the 'a'..='z' arm. Both would match 'e', but the first arm wins, so the order decides the result.
Common errors
- Missing catch-all. Integers and characters have too many values to list. Without a
_arm or a range that covers everything, you get a "non-exhaustive patterns" error. - Unreachable arms. If an earlier arm already covers a later one, the compiler warns about an unreachable pattern. Put specific arms before general ones.
- Using a variable as a pattern. A bare name in a pattern is a new binding that matches anything. It is not a comparison with an existing variable.
fn main() {
let limit = 10;
let n = 3;
match n {
limit => println!("matched everything, limit is now {limit}"),
}
}This compiles, but limit here is a new variable that shadows the outer one. It prints matched everything, limit is now 3. To compare against a value, use a constant (const LIMIT: i32 = 10;) or a match guard, which a later chapter covers.
Exercise
In match n { 1..=5 => "low", 3 | 7 => "special", _ => "other" }, what does the code return for n = 3, and why?
Show answer
It returns "low". Arms are tested in order, and 3 falls inside 1..=5, so the first arm wins. The 3 | 7 arm is partly unreachable for 3, and the compiler may warn about it. Move the or-pattern arm above the range to return "special".