Pattern matching is a Rust runtime control-flow feature. A `match` tests a value against patterns, selects an arm, and can bind or destructure data. Every possibility must be covered, either explicitly or with a catch-all. It is a language feature, not itself a design pattern. section 3.1
Patterns let a match express several kinds of decisions:
- Match `Option` cases such as `Some` and `None`, binding a contained value when needed. section 3.1.1
- Match literals, alternatives, ranges, tuple positions, enum variants, and struct fields. section 3.1.1
- Add guards for extra conditions. The compiler can flag unreachable arms and reject patterns with incompatible types. section 3.1.1
Listing 3.1 Pattern matching an Option
fn some_or_none<T>(option: &Option<T>) {
match option {
Some(_v) => println!("is some!"), #1
None => println!("is none :("),
}
}
A small `Option` match demonstrates handling both cases. section 3.1.1
Use matching to unpack data and handle cases clearly, including nested structs and enums. It can replace combinations of `if`/`else` statements; exhaustive cases make code easier to reason about, though they do not prevent logic errors. section 3.3











