21.3 Refutable vs. Irrefutable Patterns
Rust distinguishes between refutable and irrefutable patterns:
- Refutable Patterns might fail to match. An example is
Some(x)
, which does not matchNone
. - Irrefutable Patterns are guaranteed to match. For instance,
let x = 5;
always succeeds in binding5
tox
.
Refutable patterns are only allowed where there is a way to handle a failed match: match
arms, if let
, while let
, or let else
. In contrast, irrefutable patterns occur in places that cannot handle a mismatch (e.g., a normal let
binding or function parameters).