1
0
mirror of https://github.com/google/comprehensive-rust.git synced 2025-06-24 17:56:45 +02:00

Rework introduction of pattern matching (#1843)

This commit is contained in:
Nicole L
2024-02-28 11:14:53 -08:00
committed by GitHub
parent 9b57c48615
commit c1e605df25
4 changed files with 43 additions and 31 deletions

View File

@ -0,0 +1,59 @@
---
minutes: 10
---
# Matching Values
The `match` keyword lets you match a value against one or more _patterns_. The
comparisons are done from top to bottom and the first match wins.
The patterns can be simple values, similarly to `switch` in C and C++:
```rust,editable
#[rustfmt::skip]
fn main() {
let input = 'x';
match input {
'q' => println!("Quitting"),
'a' | 's' | 'w' | 'd' => println!("Moving around"),
'0'..='9' => println!("Number input"),
key if key.is_lowercase() => println!("Lowercase: {key}"),
_ => println!("Something else"),
}
}
```
The `_` pattern is a wildcard pattern which matches any value. The expressions
_must_ be exhaustive, meaning that it covers every possibility, so `_` is often
used as the final catch-all case.
Match can be used as an expression. Just like `if`, each match arm must have the
same type. The type is the last expression of the block, if any. In the example
above, the type is `()`.
A variable in the pattern (`key` in this example) will create a binding that can
be used within the match arm.
A match guard causes the arm to match only if the condition is true.
<details>
Key Points:
- You might point out how some specific characters are being used when in a
pattern
- `|` as an `or`
- `..` can expand as much as it needs to be
- `1..=5` represents an inclusive range
- `_` is a wild card
- Match guards as a separate syntax feature are important and necessary when we
wish to concisely express more complex ideas than patterns alone would allow.
- They are not the same as separate `if` expression inside of the match arm. An
`if` expression inside of the branch block (after `=>`) happens after the
match arm is selected. Failing the `if` condition inside of that block won't
result in other arms of the original `match` expression being considered.
- The condition defined in the guard applies to every expression in a pattern
with an `|`.
</details>