summaryrefslogtreecommitdiff
path: root/Pattern matching with a match expression.md
blob: affc752ca93bc1c30c33cfbfcc6dc19b906ffcd2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
[[Rust]]

This looks like a regular function call but then followed by a block of code. It is called a match expression. A match expression consists of arms, that are the pattern to match and the code to execute.

For example if a function returns an enum it is a nice way to write each case, it is somewhat similar to a switch statement.

```rust
guess.cmp(&secret_number) {
    Ordering::Less => println!("Too small!"),
    Ordering::Greater => println!("Too big!"),
    Ordering::Equal => println!("You win!!"),
}
```