Skip to content

Commit 6f133b7

Browse files
committed
---
yaml --- r: 206799 b: refs/heads/beta c: f59f41e h: refs/heads/master i: 206797: 380d918 206795: fce9e9b 206791: ab060f0 206783: 79ee0df v: v3
1 parent 9fbe69a commit 6f133b7

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
@@ -29,7 +29,7 @@ refs/tags/0.12.0: f0c419429ef30723ceaf6b42f9b5a2aeb5d2e2d1
2929
refs/heads/automation-fail: 1bf06495443584539b958873e04cc2f864ab10e4
3030
refs/heads/batch: b7fd822592a4fb577552d93010c4a4e14f314346
3131
refs/heads/building: 126db549b038c84269a1e4fe46f051b2c15d6970
32-
refs/heads/beta: 8d50216e9dfb055d28abf9da35f91542c2e9fe90
32+
refs/heads/beta: f59f41e04c044f322285f80d17916bd207d8ed04
3333
refs/heads/windistfix: 7608dbad651f02e837ed05eef3d74a6662a6e928
3434
refs/tags/1.0.0-alpha: e42bd6d93a1d3433c486200587f8f9e12590a4d7
3535
refs/heads/tmp: 579e31929feff51dcaf8d444648eff8de735f91a

branches/beta/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)