3
3
> ** <sup >Syntax</sup >** \
4
4
> _ Pattern_ :\
5
5
>   ;  ;   ;  ; [ _ LiteralPattern_ ] \
6
+ >   ;  ; | [ _ IdentifierPattern_ ] \
6
7
>   ;  ; | [ _ WildcardPattern_ ] \
7
8
>   ;  ; | [ _ RangePattern_ ] \
8
9
>   ;  ; | [ _ ReferencePattern_ ] \
9
- >   ;  ; | [ _ IdentifierPattern_ ] \
10
10
>   ;  ; | [ _ StructPattern_ ] \
11
11
>   ;  ; | [ _ TupleStructPattern_ ] \
12
12
>   ;  ; | [ _ TuplePattern_ ] \
@@ -59,11 +59,12 @@ does four things:
59
59
Patterns are used in:
60
60
61
61
* [ ` let ` declarations] ( statements.html#let-statements )
62
- * [ Function] ( items.html#functions ) and [ closure] ( expressions.html# closure-expressions )
62
+ * [ Function] ( items/functions .html ) and [ closure] ( expressions/ closure-expr.html )
63
63
parameters
64
- * [ ` match ` expressions] ( expressions.html#match-expressions )
65
- * [ ` if let ` expressions] ( expressions.html#if-let-expressions )
66
- * [ ` while let ` expressions] ( expressions.html#while-let-loops )
64
+ * [ ` match ` expressions] ( expressions/match-expr.html )
65
+ * [ ` if let ` expressions] ( expressions/if-expr.html )
66
+ * [ ` while let ` expressions] ( expressions/loop-expr.html#predicate-pattern-loops )
67
+ * [ ` for ` expressions] ( expressions/loop-expr.html#iterator-loops )
67
68
* Inside other patterns
68
69
69
70
## Destructuring
@@ -73,7 +74,7 @@ breaks a value up into its component pieces. The syntax used is almost the same
73
74
when creating such values. When destructing a data structure with named (but
74
75
not numbered) fields, it is allowed to write ` fieldname ` as a shorthand for
75
76
` fieldname: fieldname ` . In a pattern whose head expression has a ` struct ` ,
76
- ` enum ` or ` tupl ` type, a placeholder (` _ ` ) stands for a * single* data field,
77
+ ` enum ` or ` tuple ` type, a placeholder (` _ ` ) stands for a * single* data field,
77
78
whereas a wildcard ` .. ` stands for * all* the remaining fields of a particular variant.
78
79
79
80
``` rust
@@ -158,6 +159,88 @@ for i in -2..5 {
158
159
}
159
160
```
160
161
162
+ ## Identifier patterns
163
+
164
+ > ** <sup >Syntax</sup >** \
165
+ > _ IdentifierPattern_ :\
166
+ >   ;  ;   ;  ; ` ref ` <sup >?</sup > ` mut ` <sup >?</sup > IDENTIFIER (` @ ` [ _ Pattern_ ] ) <sup >?</sup >
167
+
168
+ _ Identifier patterns_ bind the value they match to a ** previously undeclared** variable.
169
+
170
+ Patterns that consist of only an identifier, possibly with a ` mut ` , like
171
+ ` variable ` , ` x ` , and ` y ` below:
172
+
173
+ ``` rust
174
+ let mut variable = 10 ;
175
+ fn sum (x : i32 , y : i32 ) -> i32 {
176
+ # x + y
177
+ # }
178
+ ```
179
+
180
+ match any value and bind it to that identifier. This is the most commonly
181
+ used pattern in variable declarations and function/closure parameters.
182
+
183
+ To bind non-trivial patterns to a variable, the use of the syntax `variable @
184
+ subpattern` is needed. For example:
185
+
186
+ ``` rust
187
+ let x = 2 ;
188
+
189
+ match x {
190
+ e @ 1 ..= 5 => println! (" got a range element {}" , e ),
191
+ _ => println! (" anything" ),
192
+ }
193
+ ```
194
+
195
+ binds to ` e ` the value 2 (not the entire range: the range here is a range subpattern).
196
+
197
+ By default, identifier patterns bind a variable to a copy of or move from the
198
+ matched value (depending whether the matched value implements the [ Copy trait] ).
199
+ This can be changed to bind to a reference by using the ` ref ` keyword,
200
+ or to a mutable reference using ` ref mut ` . For example:
201
+
202
+ ``` rust
203
+ # let a = Some (10 );
204
+ match a {
205
+ None => (),
206
+ Some (value ) => (),
207
+ }
208
+
209
+ match a {
210
+ None => (),
211
+ Some (ref value ) => (),
212
+ }
213
+ ```
214
+
215
+ in the first match expression, the value is copied (or moved). In the second match,
216
+ a reference to the same memory location is bound to the variable value. This syntax is
217
+ needed because in destructuring subpatterns we can't apply the ` & ` operator to
218
+ the value's fields. For example:
219
+
220
+ ``` rust,compile_fail
221
+ # struct Person {
222
+ # name: String,
223
+ # age: u8,
224
+ # }
225
+ # let value = Person{ name: String::from("John"), age: 23 };
226
+ if let Person{name: &person_name, age: 18..=150} = value { }
227
+ ```
228
+
229
+ is not valid. What we must do is:
230
+
231
+ ``` rust
232
+ # struct Person {
233
+ # name : String ,
234
+ # age : u8 ,
235
+ # }
236
+ # let value = Person { name : String :: from (" John" ), age : 23 };
237
+ if let Person {name : ref person_name , age : 18 ..= 150 } = value { }
238
+ ```
239
+
240
+ Thus, ` ref ` is not something that is being matched against. Its objective is
241
+ exclusively to make the matched binding a reference, instead of potentially
242
+ copying or moving what was matched.
243
+
161
244
## Wildcard pattern
162
245
163
246
> ** <sup >Syntax</sup >** \
@@ -166,7 +249,8 @@ for i in -2..5 {
166
249
167
250
The _ wildcard pattern_ matches any value. It is used to ignore values when they don't
168
251
matter. Inside other patterns it matches a single data field (as opposed to the ` .. `
169
- which matches the remaining fields).
252
+ which matches the remaining fields). Unlike identifier patterns, it does not copy, move
253
+ or borrow the value it matches.
170
254
171
255
Examples:
172
256
@@ -220,8 +304,8 @@ Range patterns match values that are within the closed range defined by its lowe
220
304
upper bounds. For example, a pattern ` 'm'..='p' ` will match only the values ` 'm' ` , ` 'n' ` ,
221
305
` 'o' ` , and ` 'p' ` . The bounds can be literals or paths that point to constant values.
222
306
223
- A pattern a ` ..= ` b must always have a &le ; b. Thus, it is not possible to have a range
224
- pattern ` 10..=0 ` , for example.
307
+ A pattern a ` ..= ` b must always have a &le ; b. It is an error to have a range pattern
308
+ ` 10..=0 ` , for example.
225
309
226
310
The ` ... ` syntax is kept for backwards compatibility.
227
311
@@ -300,7 +384,6 @@ println!("{}", match 0xfacade {
300
384
0 ..= <u32 as MaxValue >:: MAX => " fits in a u32" ,
301
385
_ => " too big" ,
302
386
});
303
-
304
387
```
305
388
306
389
Range patterns are always refutable, even when they cover the complete set
@@ -332,89 +415,6 @@ because it is a token by itself, not two `&` tokens.
332
415
333
416
Reference patterns are always irrefutable.
334
417
335
- ## Identifier patterns
336
-
337
- > ** <sup >Syntax</sup >** \
338
- > _ IdentifierPattern_ :\
339
- >   ;  ;   ;  ; ` mut ` <sup >?</sup > IDENTIFIER (` @ ` [ _ Pattern_ ] ) <sup >?</sup >\
340
- >   ;  ; | ` ref ` ` mut ` <sup >?</sup > IDENTIFIER (` @ ` [ _ Pattern_ ] ) <sup >?</sup >
341
-
342
- _ Identifier patterns_ bind the value they match to a ** previously undeclared** variable.
343
-
344
- Patterns that consist of only an identifier, possibly with a ` mut ` , like
345
- ` variable ` , ` x ` , and ` y ` below:
346
-
347
- ``` rust
348
- let mut variable = 10 ;
349
- fn sum (x : i32 , y : i32 ) -> i32 {
350
- # x + y
351
- # }
352
- ```
353
-
354
- match any value and bind it to that identifier. This is the most commonly
355
- used pattern in variable declarations and function/closure parameters.
356
-
357
- To bind non-trivial patterns to a variable, the use of the syntax `variable @
358
- subpattern` is needed. For example:
359
-
360
- ``` rust
361
- let x = 2 ;
362
-
363
- match x {
364
- e @ 1 ..= 5 => println! (" got a range element {}" , e ),
365
- _ => println! (" anything" ),
366
- }
367
- ```
368
-
369
- binds to ` e ` the value 2 (not the entire range: the range here is a range subpattern).
370
-
371
- By default, identifier patterns bind a variable to a copy of or move from the
372
- matched value (depending whether the matched value implements the [ Copy trait] ).
373
- This can be changed to bind to a reference by using the ` ref ` keyword,
374
- or to a mutable reference using ` ref mut ` . For example:
375
-
376
- ``` rust
377
- # let a = Some (10 );
378
- match a {
379
- None => (),
380
- Some (value ) => (),
381
- }
382
-
383
- match a {
384
- None => (),
385
- Some (ref value ) => (),
386
- }
387
- ```
388
-
389
- in the first match expression, the value is copied (or moved). In the second match,
390
- a reference to the same memory location is bound to the variable value. This syntax is
391
- needed because in destructuring subpatterns we can't apply the ` & ` operator to
392
- the value's fields. For example:
393
-
394
- ``` rust,compile_fail
395
- # struct Person {
396
- # name: String,
397
- # age: u8,
398
- # }
399
- # let value = Person{ name: String::from("John"), age: 23 };
400
- if let Person{& name: person_name, age: 18..=150} = value { }
401
- ```
402
-
403
- is not valid. What we must do is:
404
-
405
- ``` rust
406
- # struct Person {
407
- # name : String ,
408
- # age : u8 ,
409
- # }
410
- # let value = Person { name : String :: from (" John" ), age : 23 };
411
- if let Person {name : ref person_name , age : 18 ..= 150 } = value { }
412
- ```
413
-
414
- Thus, ` ref ` is not something that is being matched against. Its objective is
415
- exclusively to make the matched binding a reference, instead of potentially
416
- copying or moving what was matched.
417
-
418
418
## Struct patterns
419
419
420
420
> ** <sup >Syntax</sup >** \
@@ -443,9 +443,9 @@ copying or moving what was matched.
443
443
>   ;  ; ` .. `
444
444
445
445
Struct patterns match struct values that match all criteria defined by its subpatterns.
446
- They are also used to [ destructure] ( destructuring ) a struct.
446
+ They are also used to [ destructure] ( # destructuring) a struct.
447
447
448
- On a struct pattern, the fields are referenced by name, index (in the case of tuples
448
+ On a struct pattern, the fields are referenced by name, index (in the case of tuple
449
449
structs) or ignored by use of ` .. ` :
450
450
451
451
``` rust
@@ -522,7 +522,7 @@ A struct pattern is refutable when one of its subpatterns is refutable.
522
522
>   ;  ; | ([ _ Pattern_ ] ` , ` )<sup >\* </sup > ` .. ` ( (` , ` [ _ Pattern_ ] )<sup >+</sup > ` , ` <sup >?</sup > )<sup >?</sup >
523
523
524
524
TupleStruct patterns match tuple struct and enum values that match all criteria defined
525
- by its subpatterns. They are also used to [ destructure] ( destructuring ) a tuple struct or
525
+ by its subpatterns. They are also used to [ destructure] ( # destructuring) a tuple struct or
526
526
enum value.
527
527
528
528
A TupleStruct pattern is refutable when one of its subpatterns is refutable.
@@ -539,7 +539,7 @@ A TupleStruct pattern is refutable when one of its subpatterns is refutable.
539
539
>   ;  ; | ([ _ Pattern_ ] ` , ` )<sup >\* </sup > ` .. ` ( (` , ` [ _ Pattern_ ] )<sup >+</sup > ` , ` <sup >?</sup > )<sup >?</sup >
540
540
541
541
Tuple patterns match tuple values that match all criteria defined by its subpatterns.
542
- They are also used to [ destructure] ( destructuring ) a tuple.
542
+ They are also used to [ destructure] ( # destructuring) a tuple.
543
543
544
544
This pattern is refutable when one of its subpatterns is refutable.
545
545
@@ -587,8 +587,9 @@ Unqualified path patterns can refer to:
587
587
588
588
Qualified path patterns can only refer to associated constants.
589
589
590
- Path patterns are irrefutable when they refer to constants or structs.
591
- They are refutable when the refer to enum variants.
590
+ Path patterns are irrefutable when they refer to structs or an enum variant when the enum
591
+ has only one variant or a constant whose type is irrefutable. They are refutable when they
592
+ refer to refutable constants or enum variants for enums with multiple variants.
592
593
593
594
[ _Pattern_ ] : #patterns
594
595
[ _LiteralPattern_ ] : #literal-patterns
0 commit comments