2
2
3
3
> ** <sup >Syntax</sup >** \
4
4
> _ Pattern_ :\
5
+ >   ;  ;   ;  ; _ PatternWithoutRange_ \
6
+ >   ;  ; | [ _ RangePattern_ ]
7
+ >
8
+ > _ PatternWithoutRange_ :\
5
9
>   ;  ;   ;  ; [ _ LiteralPattern_ ] \
6
10
>   ;  ; | [ _ IdentifierPattern_ ] \
7
11
>   ;  ; | [ _ WildcardPattern_ ] \
8
- >   ;  ; | [ _ RangePattern_ ] \
12
+ >   ;  ; | [ _ RestPattern_ ] \
13
+ >   ;  ; | [ _ ObsoleteRangePattern_ ] \
9
14
>   ;  ; | [ _ ReferencePattern_ ] \
10
15
>   ;  ; | [ _ StructPattern_ ] \
11
16
>   ;  ; | [ _ TupleStructPattern_ ] \
@@ -248,6 +253,9 @@ copying or moving what was matched.
248
253
[ Path patterns] ( #path-patterns ) take precedence over identifier patterns. It is an error
249
254
if ` ref ` or ` ref mut ` is specified and the identifier shadows a constant.
250
255
256
+ Identifier patterns are irrefutable if the ` @ ` subpattern is irrefutable or
257
+ the subpattern is not specified.
258
+
251
259
### Binding modes
252
260
253
261
To service better ergonomics, patterns operate in different * binding modes* in
@@ -317,12 +325,67 @@ if let Some(_) = x {}
317
325
318
326
The wildcard pattern is always irrefutable.
319
327
328
+ ## Rest patterns
329
+
330
+ > ** <sup >Syntax</sup >** \
331
+ > _ RestPattern_ :\
332
+ >   ;  ; ` .. `
333
+
334
+ The _ rest pattern_ (the ` .. ` token) acts as a variable-length pattern which
335
+ matches zero or more elements that haven't been matched already before and
336
+ after. It may only be used in [ tuple] ( #tuple-patterns ) , [ tuple
337
+ struct] ( #tuple-struct-patterns ) , and [ slice] ( #slice-patterns ) patterns, and
338
+ may only appear once as one of the elements in those patterns. It is also
339
+ allowed in an [ identifier pattern] ( #identifier-patterns ) for [ slice
340
+ patterns] ( #slice-patterns ) only.
341
+
342
+ The rest pattern is always irrefutable.
343
+
344
+ Examples:
345
+
346
+ ``` rust
347
+ # let words = vec! [" a" , " b" , " c" ];
348
+ # let slice = & words [.. ];
349
+ match slice {
350
+ [] => println! (" slice is empty" ),
351
+ [one ] => println! (" single element {}" , one ),
352
+ [head , tail @ .. ] => println! (" head={} tail={:?}" , head , tail ),
353
+ }
354
+
355
+ match slice {
356
+ // Ignore everything but the last element, which must be "!".
357
+ [.. , " !" ] => println! (" !!!" ),
358
+
359
+ // `start` is a slice of everything except the last element, which must be "z".
360
+ [start @ .. , " z" ] => println! (" starts with: {:?}" , start ),
361
+
362
+ // `end` is a slice of everything but the first element, which must be "a".
363
+ [" a" , end @ .. ] => println! (" ends with: {:?}" , end ),
364
+
365
+ rest => println! (" {:?}" , rest ),
366
+ }
367
+
368
+ if let [.. , penultimate , _ ] = slice {
369
+ println! (" next to last is {}" , penultimate );
370
+ }
371
+
372
+ # let tuple = (1 , 2 , 3 , 4 , 5 );
373
+ // Rest patterns may also be used in tuple and tuple struct patterns.
374
+ match tuple {
375
+ (1 , .. , y , z ) => println! (" y={} z={}" , y , z ),
376
+ (.. , 5 ) => println! (" tail must be 5" ),
377
+ (.. ) => println! (" matches everything else" ),
378
+ }
379
+ ```
380
+
320
381
## Range patterns
321
382
322
383
> ** <sup >Syntax</sup >** \
323
384
> _ RangePattern_ :\
324
- >   ;  ;  ;  ; _ RangePatternBound_ ` ..= ` _ RangePatternBound_ \
325
- >   ;  ; | _ RangePatternBound_ ` ... ` _ RangePatternBound_
385
+ >   ;  ; _ RangePatternBound_ ` ..= ` _ RangePatternBound_
386
+ >
387
+ > _ ObsoleteRangePattern_ :\
388
+ >   ;  ; _ RangePatternBound_ ` ... ` _ RangePatternBound_
326
389
>
327
390
> _ RangePatternBound_ :\
328
391
>   ;  ;   ;  ; [ CHAR_LITERAL] \
@@ -429,7 +492,7 @@ ranges containing all Unicode Scalar Values: `'\u{0000}'..='\u{D7FF}'` and
429
492
430
493
> ** <sup >Syntax</sup >** \
431
494
> _ ReferencePattern_ :\
432
- >   ;  ; (` & ` |` && ` ) ` mut ` <sup >?</sup > _ Pattern _
495
+ >   ;  ; (` & ` |` && ` ) ` mut ` <sup >?</sup > [ _ PatternWithoutRange _ ]
433
496
434
497
Reference patterns dereference the pointers that are being matched
435
498
and, thus, borrow them.
@@ -559,8 +622,7 @@ A struct pattern is refutable when one of its subpatterns is refutable.
559
622
>   ;  ; [ _ PathInExpression_ ] ` ( ` _ TupleStructItems_ <sup >?</sup > ` ) `
560
623
>
561
624
> _ TupleStructItems_ :\
562
- >   ;  ;   ;  ; [ _ Pattern_ ]   ; ( ` , ` [ _ Pattern_ ] )<sup >\* </sup > ` , ` <sup >?</sup >\
563
- >   ;  ; | ([ _ Pattern_ ] ` , ` )<sup >\* </sup > ` .. ` (` , ` [ _ Pattern_ ] )<sup >* </sup > ` , ` <sup >?</sup >
625
+ >   ;  ; [ _ Pattern_ ]   ; ( ` , ` [ _ Pattern_ ] )<sup >\* </sup > ` , ` <sup >?</sup >
564
626
565
627
Tuple struct patterns match tuple struct and enum values that match all criteria defined
566
628
by its subpatterns. They are also used to [ destructure] ( #destructuring ) a tuple struct or
@@ -576,13 +638,16 @@ A tuple struct pattern is refutable when one of its subpatterns is refutable.
576
638
>
577
639
> _ TuplePatternItems_ :\
578
640
>   ;  ;   ;  ; [ _ Pattern_ ] ` , ` \
579
- >   ;  ; | [ _ Pattern _ ] & nbsp ; ( ` , ` [ _ Pattern _ ] )< sup >+</ sup > ` , ` < sup >?</ sup > \
580
- >   ;  ; | ( [ _ Pattern_ ] ` , ` )< sup > \* </ sup > ` .. ` (` , ` [ _ Pattern_ ] )<sup >* </sup > ` , ` <sup >?</sup >
641
+ >   ;  ; | [ _ RestPattern _ ] \
642
+ >   ;  ; | [ _ Pattern_ ] & nbsp ; (` , ` [ _ Pattern_ ] )<sup >+ </sup > ` , ` <sup >?</sup >
581
643
582
644
Tuple patterns match tuple values that match all criteria defined by its subpatterns.
583
645
They are also used to [ destructure] ( #destructuring ) a tuple.
584
646
585
- This pattern is refutable when one of its subpatterns is refutable.
647
+ The form ` (..) ` with a single [ _ RestPattern_ ] is a special form that does not
648
+ require a comma, and matches a tuple of any size.
649
+
650
+ The tuple pattern is refutable when one of its subpatterns is refutable.
586
651
587
652
## Grouped patterns
588
653
@@ -607,7 +672,10 @@ match int_reference {
607
672
608
673
> ** <sup >Syntax</sup >** \
609
674
> _ SlicePattern_ :\
610
- >   ;  ; ` [ ` [ _ Pattern_ ] \( ` , ` [ _ Pattern_ ] )<sup >\* </sup > ` , ` <sup >?</sup > ` ] `
675
+ >   ;  ; ` [ ` _ SlicePatternItems_ <sup >?</sup > ` ] `
676
+ >
677
+ > _ SlicePatternItems_ :\
678
+ >   ;  ; [ _ Pattern_ ] \( ` , ` [ _ Pattern_ ] )<sup >\* </sup > ` , ` <sup >?</sup >
611
679
612
680
Slice patterns can match both arrays of fixed size and slices of dynamic size.
613
681
``` rust
@@ -628,6 +696,11 @@ match v[..] {
628
696
};
629
697
```
630
698
699
+ Slice patterns are irrefutable when matching an array as long as each element
700
+ is irrefutable. When matching a slice, it is irrefutable only in the form with
701
+ a single ` .. ` [ rest pattern] ( #rest-patterns ) or [ identifier
702
+ pattern] ( #identifier-patterns ) with the ` .. ` rest pattern as a subpattern.
703
+
631
704
## Path patterns
632
705
633
706
> ** <sup >Syntax</sup >** \
@@ -658,12 +731,15 @@ refer to refutable constants or enum variants for enums with multiple variants.
658
731
[ _IdentifierPattern_ ] : #identifier-patterns
659
732
[ _LiteralPattern_ ] : #literal-patterns
660
733
[ _MacroInvocation_ ] : macros.md#macro-invocation
734
+ [ _ObsoleteRangePattern_ ] : #range-patterns
661
735
[ _PathInExpression_ ] : paths.md#paths-in-expressions
662
736
[ _PathPattern_ ] : #path-patterns
663
737
[ _Pattern_ ] : #patterns
738
+ [ _PatternWithoutRange_ ] : #patterns
664
739
[ _QualifiedPathInExpression_ ] : paths.md#qualified-paths
665
740
[ _RangePattern_ ] : #range-patterns
666
741
[ _ReferencePattern_ ] : #reference-patterns
742
+ [ _RestPattern_ ] : #rest-patterns
667
743
[ _SlicePattern_ ] : #slice-patterns
668
744
[ _StructPattern_ ] : #struct-patterns
669
745
[ _TuplePattern_ ] : #tuple-patterns
0 commit comments