@@ -21,6 +21,8 @@ match x {
21
21
}
22
22
```
23
23
24
+ This prints ` one ` .
25
+
24
26
# Multiple patterns
25
27
26
28
You can match multiple patterns with ` | ` :
@@ -35,6 +37,8 @@ match x {
35
37
}
36
38
```
37
39
40
+ This prints ` one or two ` .
41
+
38
42
# Ranges
39
43
40
44
You can match a range of values with ` ... ` :
@@ -48,7 +52,21 @@ match x {
48
52
}
49
53
```
50
54
51
- Ranges are mostly used with integers and single characters.
55
+ This prints ` one through five ` .
56
+
57
+ Ranges are mostly used with integers and ` char ` s:
58
+
59
+ ``` rust
60
+ let x = '💅' ;
61
+
62
+ match x {
63
+ 'a' ... 'j' => println! (" early letter" ),
64
+ 'k' ... 'z' => println! (" late letter" ),
65
+ _ => println! (" something else" ),
66
+ }
67
+ ```
68
+
69
+ This prints ` something else `
52
70
53
71
# Bindings
54
72
@@ -64,6 +82,8 @@ match x {
64
82
}
65
83
```
66
84
85
+ This prints ` got a range element 1 ` .
86
+
67
87
# Ignoring variants
68
88
69
89
If you’re matching on an enum which has variants, you can use ` .. ` to
@@ -83,6 +103,8 @@ match x {
83
103
}
84
104
```
85
105
106
+ This prints ` Got an int! ` .
107
+
86
108
# Guards
87
109
88
110
You can introduce ‘match guards’ with ` if ` :
@@ -102,6 +124,8 @@ match x {
102
124
}
103
125
```
104
126
127
+ This prints ` Got an int! `
128
+
105
129
# ref and ref mut
106
130
107
131
If you want to get a [ reference] [ ref ] , use the ` ref ` keyword:
@@ -114,6 +138,8 @@ match x {
114
138
}
115
139
```
116
140
141
+ This prints ` Got a reference to 5 ` .
142
+
117
143
[ ref ] : references-and-borrowing.html
118
144
119
145
Here, the ` r ` inside the ` match ` has the type ` &i32 ` . In other words, the ` ref `
@@ -130,7 +156,7 @@ match x {
130
156
131
157
# Destructuring
132
158
133
- If you have a compound data type, like a ` struct ` , you can destructure it
159
+ If you have a compound data type, like a [ ` struct ` ] [ struct ] , you can destructure it
134
160
inside of a pattern:
135
161
136
162
``` rust
@@ -146,6 +172,8 @@ match origin {
146
172
}
147
173
```
148
174
175
+ [ struct ] : structs.html
176
+
149
177
If we only care about some of the values, we don’t have to give them all names:
150
178
151
179
``` rust
@@ -161,6 +189,8 @@ match origin {
161
189
}
162
190
```
163
191
192
+ This prints ` x is 0 ` .
193
+
164
194
You can do this kind of match on any member, not just the first:
165
195
166
196
``` rust
@@ -176,6 +206,8 @@ match origin {
176
206
}
177
207
```
178
208
209
+ This prints ` y is 0 ` .
210
+
179
211
This ‘destructuring’ behavior works on any compound data type, like
180
212
[ tuples] [ tuples ] or [ enums] [ enums ] .
181
213
@@ -187,10 +219,10 @@ This ‘destructuring’ behavior works on any compound data type, like
187
219
Whew! That’s a lot of different ways to match things, and they can all be
188
220
mixed and matched, depending on what you’re doing:
189
221
190
- ``` { rust,ignore}
222
+ ``` rust,ignore
191
223
match x {
192
224
Foo { x: Some(ref name), y: None } => ...
193
225
}
194
226
```
195
227
196
- Patterns are very powerful. Make good use of them.
228
+ Patterns are very powerful. Make good use of them.
0 commit comments