@@ -112,26 +112,55 @@ match x {
112
112
}
113
113
```
114
114
115
- # Ignoring variants
115
+ # Ignoring bindings
116
116
117
- If you’re matching on an enum which has variants, you can use ` .. ` to
118
- ignore the value and type in the variant :
117
+ You can use ` _ ` in a pattern to disregard the type and value.
118
+ For example, here’s a ` match ` against a ` Result<T, E> ` :
119
119
120
120
``` rust
121
- enum OptionalInt {
122
- Value (i32 ),
121
+ # let some_value : Result <i32 , & 'static str > = Err (" There was an error" );
122
+ match some_value {
123
+ Ok (value ) => println! (" got a value: {}" , value ),
124
+ Err (_ ) => println! (" an error occurred" ),
125
+ }
126
+ ```
127
+
128
+ In the first arm, we bind the value inside the ` Ok ` variant to ` value ` . But
129
+ in the ` Err ` arm, we use ` _ ` to disregard the specific error, and just print
130
+ a general error message.
131
+
132
+ ` _ ` is valid in any pattern that creates a binding. This can be useful to
133
+ ignore parts of a larger structure:
134
+
135
+ ``` rust
136
+ fn coordinate () -> (i32 , i32 , i32 ) {
137
+ // generate and return some sort of triple tuple
138
+ # (1 , 2 , 3 )
139
+ }
140
+
141
+ let (x , _ , z ) = coordinate ();
142
+ ```
143
+
144
+ Here, we bind the first and last element of the tuple to ` x ` and ` z ` , but
145
+ ignore the middle element.
146
+
147
+ Similarly, you can use ` .. ` in a pattern to disregard multiple values.
148
+
149
+ ``` rust
150
+ enum OptionalTuple {
151
+ Value (i32 , i32 , i32 ),
123
152
Missing ,
124
153
}
125
154
126
- let x = OptionalInt :: Value (5 );
155
+ let x = OptionalTuple :: Value (5 , - 2 , 3 );
127
156
128
157
match x {
129
- OptionalInt :: Value (.. ) => println! (" Got an int !" ),
130
- OptionalInt :: Missing => println! (" No such luck." ),
158
+ OptionalTuple :: Value (.. ) => println! (" Got a tuple !" ),
159
+ OptionalTuple :: Missing => println! (" No such luck." ),
131
160
}
132
161
```
133
162
134
- This prints ` Got an int ! ` .
163
+ This prints ` Got a tuple ! ` .
135
164
136
165
# Guards
137
166
@@ -282,38 +311,6 @@ This ‘destructuring’ behavior works on any compound data type, like
282
311
[ tuples ] : primitive-types.html#tuples
283
312
[ enums ] : enums.html
284
313
285
- # Ignoring bindings
286
-
287
- You can use ` _ ` in a pattern to disregard the value. For example, here’s a
288
- ` match ` against a ` Result<T, E> ` :
289
-
290
- ``` rust
291
- # let some_value : Result <i32 , & 'static str > = Err (" There was an error" );
292
- match some_value {
293
- Ok (value ) => println! (" got a value: {}" , value ),
294
- Err (_ ) => println! (" an error occurred" ),
295
- }
296
- ```
297
-
298
- In the first arm, we bind the value inside the ` Ok ` variant to ` value ` . But
299
- in the ` Err ` arm, we use ` _ ` to disregard the specific error, and just print
300
- a general error message.
301
-
302
- ` _ ` is valid in any pattern that creates a binding. This can be useful to
303
- ignore parts of a larger structure:
304
-
305
- ``` rust
306
- fn coordinate () -> (i32 , i32 , i32 ) {
307
- // generate and return some sort of triple tuple
308
- # (1 , 2 , 3 )
309
- }
310
-
311
- let (x , _ , z ) = coordinate ();
312
- ```
313
-
314
- Here, we bind the first and last element of the tuple to ` x ` and ` z ` , but
315
- ignore the middle element.
316
-
317
314
# Mix and Match
318
315
319
316
Whew! That’s a lot of different ways to match things, and they can all be
0 commit comments