@@ -39,79 +39,81 @@ match x {
39
39
40
40
This prints ` one or two ` .
41
41
42
- # Ranges
42
+ # Destructuring
43
43
44
- You can match a range of values with ` ... ` :
44
+ If you have a compound data type, like a [ ` struct ` ] [ struct ] , you can destructure it
45
+ inside of a pattern:
45
46
46
47
``` rust
47
- let x = 1 ;
48
-
49
- match x {
50
- 1 ... 5 => println! (" one through five" ),
51
- _ => println! (" anything" ),
48
+ struct Point {
49
+ x : i32 ,
50
+ y : i32 ,
52
51
}
53
- ```
54
-
55
- This prints ` one through five ` .
56
52
57
- Ranges are mostly used with integers and ` char ` s:
58
-
59
- ``` rust
60
- let x = '💅' ;
53
+ let origin = Point { x : 0 , y : 0 };
61
54
62
- match x {
63
- 'a' ... 'j' => println! (" early letter" ),
64
- 'k' ... 'z' => println! (" late letter" ),
65
- _ => println! (" something else" ),
55
+ match origin {
56
+ Point { x , y } => println! (" ({},{})" , x , y ),
66
57
}
67
58
```
68
59
69
- This prints ` something else ` .
70
-
71
- # Bindings
60
+ [ struct ] : structs.html
72
61
73
- You can bind values to names with ` @ ` :
62
+ We can use ` : ` to give a value a different name.
74
63
75
64
``` rust
76
- let x = 1 ;
65
+ struct Point {
66
+ x : i32 ,
67
+ y : i32 ,
68
+ }
77
69
78
- match x {
79
- e @ 1 ... 5 => println! (" got a range element {}" , e ),
80
- _ => println! (" anything" ),
70
+ let origin = Point { x : 0 , y : 0 };
71
+
72
+ match origin {
73
+ Point { x : x1 , y : y1 } => println! (" ({},{})" , x1 , y1 ),
81
74
}
82
75
```
83
76
84
- This prints ` got a range element 1 ` . This is useful when you want to
85
- do a complicated match of part of a data structure:
77
+ If we only care about some of the values, we don’t have to give them all names:
86
78
87
79
``` rust
88
- #[derive( Debug )]
89
- struct Person {
90
- name : Option < String > ,
80
+ struct Point {
81
+ x : i32 ,
82
+ y : i32 ,
91
83
}
92
84
93
- let name = " Steve" . to_string ();
94
- let mut x : Option <Person > = Some (Person { name : Some (name ) });
95
- match x {
96
- Some (Person { name : ref a @ Some (_ ), .. }) => println! (" {:?}" , a ),
97
- _ => {}
85
+ let origin = Point { x : 0 , y : 0 };
86
+
87
+ match origin {
88
+ Point { x , .. } => println! (" x is {}" , x ),
98
89
}
99
90
```
100
91
101
- This prints ` Some("Steve") ` : We’ve bound the inner ` name ` to ` a ` .
92
+ This prints ` x is 0 ` .
102
93
103
- If you use ` @ ` with ` | ` , you need to make sure the name is bound in each part
104
- of the pattern:
94
+ You can do this kind of match on any member, not just the first:
105
95
106
96
``` rust
107
- let x = 5 ;
97
+ struct Point {
98
+ x : i32 ,
99
+ y : i32 ,
100
+ }
108
101
109
- match x {
110
- e @ 1 ... 5 | e @ 8 ... 10 => println! (" got a range element {}" , e ),
111
- _ => println! (" anything" ),
102
+ let origin = Point { x : 0 , y : 0 };
103
+
104
+ match origin {
105
+ Point { y , .. } => println! (" y is {}" , y ),
112
106
}
113
107
```
114
108
109
+ This prints ` y is 0 ` .
110
+
111
+ This ‘destructuring’ behavior works on any compound data type, like
112
+ [ tuples] [ tuples ] or [ enums] [ enums ] .
113
+
114
+ [ tuples ] : primitive-types.html#tuples
115
+ [ enums ] : enums.html
116
+
115
117
# Ignoring bindings
116
118
117
119
You can use ` _ ` in a pattern to disregard the type and value.
@@ -162,154 +164,152 @@ match x {
162
164
163
165
This prints ` Got a tuple! ` .
164
166
165
- # Guards
167
+ # ref and ref mut
166
168
167
- You can introduce ‘match guards’ with ` if ` :
169
+ If you want to get a [ reference ] [ ref ] , use the ` ref ` keyword :
168
170
169
171
``` rust
170
- enum OptionalInt {
171
- Value (i32 ),
172
- Missing ,
173
- }
174
-
175
- let x = OptionalInt :: Value (5 );
172
+ let x = 5 ;
176
173
177
174
match x {
178
- OptionalInt :: Value (i ) if i > 5 => println! (" Got an int bigger than five!" ),
179
- OptionalInt :: Value (.. ) => println! (" Got an int!" ),
180
- OptionalInt :: Missing => println! (" No such luck." ),
175
+ ref r => println! (" Got a reference to {}" , r ),
181
176
}
182
177
```
183
178
184
- This prints ` Got an int! ` .
179
+ This prints ` Got a reference to 5 ` .
185
180
186
- If you’re using ` if ` with multiple patterns, the ` if ` applies to both sides:
181
+ [ ref ] : references-and-borrowing.html
182
+
183
+ Here, the ` r ` inside the ` match ` has the type ` &i32 ` . In other words, the ` ref `
184
+ keyword _ creates_ a reference, for use in the pattern. If you need a mutable
185
+ reference, ` ref mut ` will work in the same way:
187
186
188
187
``` rust
189
- let x = 4 ;
190
- let y = false ;
188
+ let mut x = 5 ;
191
189
192
190
match x {
193
- 4 | 5 if y => println! (" yes" ),
194
- _ => println! (" no" ),
191
+ ref mut mr => println! (" Got a mutable reference to {}" , mr ),
195
192
}
196
193
```
197
194
198
- This prints ` no ` , because the ` if ` applies to the whole of ` 4 | 5 ` , and not to
199
- just the ` 5 ` , In other words, the the precedence of ` if ` behaves like this:
195
+ # Ranges
200
196
201
- ``` text
202
- (4 | 5) if y => ...
203
- ```
197
+ You can match a range of values with ` ... ` :
204
198
205
- not this:
199
+ ``` rust
200
+ let x = 1 ;
206
201
207
- ``` text
208
- 4 | (5 if y) => ...
202
+ match x {
203
+ 1 ... 5 => println! (" one through five" ),
204
+ _ => println! (" anything" ),
205
+ }
209
206
```
210
207
211
- # ref and ref mut
208
+ This prints ` one through five ` .
212
209
213
- If you want to get a [ reference ] [ ref ] , use the ` ref ` keyword :
210
+ Ranges are mostly used with integers and ` char ` s :
214
211
215
212
``` rust
216
- let x = 5 ;
213
+ let x = '💅' ;
217
214
218
215
match x {
219
- ref r => println! (" Got a reference to {}" , r ),
216
+ 'a' ... 'j' => println! (" early letter" ),
217
+ 'k' ... 'z' => println! (" late letter" ),
218
+ _ => println! (" something else" ),
220
219
}
221
220
```
222
221
223
- This prints ` Got a reference to 5 ` .
222
+ This prints ` something else ` .
224
223
225
- [ ref ] : references-and-borrowing.html
224
+ # Bindings
226
225
227
- Here, the ` r ` inside the ` match ` has the type ` &i32 ` . In other words, the ` ref `
228
- keyword _ creates_ a reference, for use in the pattern. If you need a mutable
229
- reference, ` ref mut ` will work in the same way:
226
+ You can bind values to names with ` @ ` :
230
227
231
228
``` rust
232
- let mut x = 5 ;
229
+ let x = 1 ;
233
230
234
231
match x {
235
- ref mut mr => println! (" Got a mutable reference to {}" , mr ),
232
+ e @ 1 ... 5 => println! (" got a range element {}" , e ),
233
+ _ => println! (" anything" ),
236
234
}
237
235
```
238
236
239
- # Destructuring
240
-
241
- If you have a compound data type, like a [ ` struct ` ] [ struct ] , you can destructure it
242
- inside of a pattern:
237
+ This prints ` got a range element 1 ` . This is useful when you want to
238
+ do a complicated match of part of a data structure:
243
239
244
240
``` rust
245
- struct Point {
246
- x : i32 ,
247
- y : i32 ,
241
+ #[derive( Debug )]
242
+ struct Person {
243
+ name : Option < String > ,
248
244
}
249
245
250
- let origin = Point { x : 0 , y : 0 };
251
-
252
- match origin {
253
- Point { x , y } => println! (" ({},{})" , x , y ),
246
+ let name = " Steve" . to_string ();
247
+ let mut x : Option <Person > = Some (Person { name : Some (name ) });
248
+ match x {
249
+ Some (Person { name : ref a @ Some (_ ), .. }) => println! (" {:?}" , a ),
250
+ _ => {}
254
251
}
255
252
```
256
253
257
- [ struct ] : structs.html
254
+ This prints ` Some("Steve") ` : We’ve bound the inner ` name ` to ` a ` .
258
255
259
- We can use ` : ` to give a value a different name.
256
+ If you use ` @ ` with ` | ` , you need to make sure the name is bound in each part
257
+ of the pattern:
260
258
261
259
``` rust
262
- struct Point {
263
- x : i32 ,
264
- y : i32 ,
265
- }
266
-
267
- let origin = Point { x : 0 , y : 0 };
260
+ let x = 5 ;
268
261
269
- match origin {
270
- Point { x : x1 , y : y1 } => println! (" ({},{})" , x1 , y1 ),
262
+ match x {
263
+ e @ 1 ... 5 | e @ 8 ... 10 => println! (" got a range element {}" , e ),
264
+ _ => println! (" anything" ),
271
265
}
272
266
```
273
267
274
- If we only care about some of the values, we don’t have to give them all names:
268
+ # Guards
269
+
270
+ You can introduce ‘match guards’ with ` if ` :
275
271
276
272
``` rust
277
- struct Point {
278
- x : i32 ,
279
- y : i32 ,
273
+ enum OptionalInt {
274
+ Value ( i32 ) ,
275
+ Missing ,
280
276
}
281
277
282
- let origin = Point { x : 0 , y : 0 } ;
278
+ let x = OptionalInt :: Value ( 5 ) ;
283
279
284
- match origin {
285
- Point { x , .. } => println! (" x is {}" , x ),
280
+ match x {
281
+ OptionalInt :: Value (i ) if i > 5 => println! (" Got an int bigger than five!" ),
282
+ OptionalInt :: Value (.. ) => println! (" Got an int!" ),
283
+ OptionalInt :: Missing => println! (" No such luck." ),
286
284
}
287
285
```
288
286
289
- This prints ` x is 0 ` .
287
+ This prints ` Got an int! ` .
290
288
291
- You can do this kind of match on any member, not just the first :
289
+ If you’re using ` if ` with multiple patterns, the ` if ` applies to both sides :
292
290
293
291
``` rust
294
- struct Point {
295
- x : i32 ,
296
- y : i32 ,
297
- }
298
-
299
- let origin = Point { x : 0 , y : 0 };
292
+ let x = 4 ;
293
+ let y = false ;
300
294
301
- match origin {
302
- Point { y , .. } => println! (" y is {}" , y ),
295
+ match x {
296
+ 4 | 5 if y => println! (" yes" ),
297
+ _ => println! (" no" ),
303
298
}
304
299
```
305
300
306
- This prints ` y is 0 ` .
301
+ This prints ` no ` , because the ` if ` applies to the whole of ` 4 | 5 ` , and not to
302
+ just the ` 5 ` , In other words, the the precedence of ` if ` behaves like this:
307
303
308
- This ‘destructuring’ behavior works on any compound data type, like
309
- [ tuples] [ tuples ] or [ enums] [ enums ] .
304
+ ``` text
305
+ (4 | 5) if y => ...
306
+ ```
310
307
311
- [ tuples ] : primitive-types.html#tuples
312
- [ enums ] : enums.html
308
+ not this:
309
+
310
+ ``` text
311
+ 4 | (5 if y) => ...
312
+ ```
313
313
314
314
# Mix and Match
315
315
0 commit comments