Sub topic 2
Refutable versus irrefutable patterns
This chapter assumes you know basic Rust syntax, the Option and Result enums, and match with simple patterns. It explains the one distinction that decides where if let, while let, and let else are allowed. Later chapters build on it.
Two kinds of pattern
A pattern is irrefutable if it matches every value of the type it is checked against. It can never fail. A pattern is refutable if some values of that type do not match it.
| Pattern | Matched against | Kind |
|---|---|---|
| `x` | any `i32` | Irrefutable |
| `(a, b)` | a `(i32, i32)` | Irrefutable |
| `Some(x)` | an `Option<i32>` | Refutable |
| `Ok(n)` | a `Result<u16, String>` | Refutable |
| `1..=5` | an `i32` | Refutable |
Refutability depends on the pattern and the type together. Some(x) is refutable against Option<i32>, because the value might be None. A pattern that names the only variant of an enum would be irrefutable, because no other value exists.
Where each kind is required
Plain let needs an irrefutable pattern. If the pattern could fail, the compiler has no code to run for the failing case, so it rejects the program.
fn main() {
let maybe: Option<i32> = Some(5);
let Some(value) = maybe;
println!("{value}");
}This does not compile. The error is E0005, "refutable pattern in local binding", and it notes that None is not covered. Function parameters and for loop patterns have the same rule.
match sits in between. Each arm may be refutable, but the arms together must cover every possible value. This is called exhaustiveness.
The constructs in this tutorial take the opposite approach. They accept a refutable pattern and give you a way to handle the case where it fails.
| Construct | Pattern it expects | If the pattern fails |
|---|---|---|
| `let` | irrefutable | not allowed |
| `match` | refutable arms, exhaustive overall | another arm runs |
| `if let` | refutable | skip the block, or run `else` |
| `while let` | refutable | the loop ends |
| `let else` | refutable | the `else` block runs and must leave |
Fixing the error
Both of these fix the example above. Each one gives the None case somewhere to go.
fn main() {
let maybe: Option<i32> = Some(5);
// Option 1: handle both cases with match.
let value = match maybe {
Some(v) => v,
None => 0,
};
println!("{value}");
// Option 2: handle the one case you care about with if let.
if let Some(v) = maybe {
println!("{v}");
}
}let else is a third option, covered in its own chapter.
A warning in the other direction
The compiler also complains when you use a refutable-pattern construct with a pattern that cannot fail.
fn main() {
let pair = (1, 2);
if let (a, b) = pair {
println!("{a} {b}");
}
}This compiles, but you get the warning "irrefutable if let pattern". The if can never be false, so a plain let (a, b) = pair; says the same thing more clearly. Treat this warning as a sign that you picked the wrong construct.
Quick check
Exercise
Which of these patterns is refutable when matched against a Result<i32, String>: r, Ok(n), or Err(_)? What happens if you write let Ok(n) = result; with plain let?
Show answer
Ok(n) and Err(_) are refutable, because each matches only one variant. r is irrefutable because it binds any value. Plain let Ok(n) = result; fails to compile with E0005, because the Err case is not covered. Use match, if let, or let else instead.
Exercise
The compiler warns "irrefutable if let pattern" for if let (x, y) = point { ... } where point is (i32, i32). Why, and what is the fix?
Show answer
A tuple pattern with plain bindings matches every (i32, i32), so the if let can never fail. Replace it with let (x, y) = point; and drop the if.