Skip to content

Commit 661f593

Browse files
committed
---
yaml --- r: 210687 b: refs/heads/try c: f59f41e h: refs/heads/master i: 210685: 81fa634 210683: 69505cc 210679: a74335d 210671: d97dfb7 210655: 7e2ec68 210623: e2c663d 210559: a6b45e6 210431: 1062752 v: v3
1 parent 050e733 commit 661f593

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
refs/heads/master: 3e561f05c00cd180ec02db4ccab2840a4aba93d2
33
refs/heads/snap-stage1: e33de59e47c5076a89eadeb38f4934f58a3618a6
44
refs/heads/snap-stage3: ba0e1cd8147d452c356aacb29fb87568ca26f111
5-
refs/heads/try: 8d50216e9dfb055d28abf9da35f91542c2e9fe90
5+
refs/heads/try: f59f41e04c044f322285f80d17916bd207d8ed04
66
refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105
77
refs/heads/dist-snap: ba4081a5a8573875fed17545846f6f6902c8ba8d
88
refs/tags/release-0.2: c870d2dffb391e14efb05aa27898f1f6333a9596

branches/try/src/doc/trpl/match.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,3 +61,40 @@ let number = match x {
6161
```
6262

6363
Sometimes it’s a nice way of converting something from one type to another.
64+
65+
# Matching on enums
66+
67+
Another important use of the `match` keyword is to process the possible
68+
variants of an enum:
69+
70+
```rust
71+
enum Message {
72+
Quit,
73+
ChangeColor(i32, i32, i32),
74+
Move { x: i32, y: i32 },
75+
Write(String),
76+
}
77+
78+
fn quit() { /* ... */ }
79+
fn change_color(r: i32, g: i32, b: i32) { /* ... */ }
80+
fn move_cursor(x: i32, y: i32) { /* ... */ }
81+
82+
fn process_message(msg: Message) {
83+
match msg {
84+
Message::Quit => quit(),
85+
Message::ChangeColor(r, g, b) => change_color(r, g, b),
86+
Message::Move { x: x, y: y } => move_cursor(x, y),
87+
Message::Write(s) => println!("{}", s),
88+
};
89+
}
90+
```
91+
92+
Again, the Rust compiler checks exhaustiveness, so it demands that you
93+
have a match arm for every variant of the enum. If you leave one off, it
94+
will give you a compile-time error unless you use `_`.
95+
96+
Unlike the previous uses of `match`, you can’t use the normal `if`
97+
statement to do this. You can use the [`if let`][if-let] statement,
98+
which can be seen as an abbreviated form of `match`.
99+
100+
[if-let][if-let.html]

0 commit comments

Comments
 (0)