5
5
>   ;  ;   ;  ; [ _ LiteralPattern_ ] \
6
6
>   ;  ; | [ _ IdentifierPattern_ ] \
7
7
>   ;  ; | [ _ WildcardPattern_ ] \
8
+ >   ;  ; | [ _ RestPattern_ ] \
8
9
>   ;  ; | [ _ RangePattern_ ] \
9
10
>   ;  ; | [ _ ReferencePattern_ ] \
10
11
>   ;  ; | [ _ StructPattern_ ] \
@@ -317,6 +318,59 @@ if let Some(_) = x {}
317
318
318
319
The wildcard pattern is always irrefutable.
319
320
321
+ ## Rest patterns
322
+
323
+ > ** <sup >Syntax</sup >** \
324
+ > _ RestPattern_ :\
325
+ >   ;  ; ` .. `
326
+
327
+ The _ rest pattern_ (the ` .. ` token) acts as a variable-length pattern which
328
+ matches zero or more elements that haven't been matched already before and
329
+ after. It may only be used in [ tuple] ( #tuple-patterns ) , [ tuple
330
+ struct] ( #tuple-struct-patterns ) , and [ slice] ( #slice-patterns ) patterns, and
331
+ may only appear once as one of the elements in those patterns. It is also
332
+ allowed in an [ identifier pattern] ( #identifier-patterns ) for [ slice
333
+ patterns] ( #slice-patterns ) only.
334
+
335
+ The rest pattern is always irrefutable.
336
+
337
+ Examples:
338
+
339
+ ``` rust
340
+ # let words = vec! [" a" , " b" , " c" ];
341
+ # let slice = & words [.. ];
342
+ match slice {
343
+ [] => println! (" slice is empty" ),
344
+ [one ] => println! (" single element {}" , one ),
345
+ [head , tail @ .. ] => println! (" head={} tail={:?}" , head , tail ),
346
+ }
347
+
348
+ match slice {
349
+ // Ignore everything but the last element, which must be "!".
350
+ [.. , " !" ] => println! (" !!!" ),
351
+
352
+ // `start` is a slice of everything except the last element, which must be "z".
353
+ [start @ .. , " z" ] => println! (" starts with: {:?}" , start ),
354
+
355
+ // `end` is a slice of everything but the first element, which must be "a".
356
+ [" a" , end @ .. ] => println! (" ends with: {:?}" , end ),
357
+
358
+ rest => println! (" {:?}" , rest ),
359
+ }
360
+
361
+ if let [.. , penultimate , _ ] = slice {
362
+ println! (" next to last is {}" , penultimate );
363
+ }
364
+
365
+ # let tuple = (1 , 2 , 3 , 4 , 5 );
366
+ // Rest patterns may also be used in tuple and tuple struct patterns.
367
+ match tuple {
368
+ (1 , .. , y , z ) => println! (" y={} z={}" , y , z ),
369
+ (.. , 5 ) => println! (" tail must be 5" ),
370
+ (.. ) => println! (" matches everything else" ),
371
+ }
372
+ ```
373
+
320
374
## Range patterns
321
375
322
376
> ** <sup >Syntax</sup >** \
@@ -559,8 +613,7 @@ A struct pattern is refutable when one of its subpatterns is refutable.
559
613
>   ;  ; [ _ PathInExpression_ ] ` ( ` _ TupleStructItems_ <sup >?</sup > ` ) `
560
614
>
561
615
> _ TupleStructItems_ :\
562
- >   ;  ;   ;  ; [ _ Pattern_ ]   ; ( ` , ` [ _ Pattern_ ] )<sup >\* </sup > ` , ` <sup >?</sup >\
563
- >   ;  ; | ([ _ Pattern_ ] ` , ` )<sup >\* </sup > ` .. ` (` , ` [ _ Pattern_ ] )<sup >* </sup > ` , ` <sup >?</sup >
616
+ >   ;  ; [ _ Pattern_ ]   ; ( ` , ` [ _ Pattern_ ] )<sup >\* </sup > ` , ` <sup >?</sup >
564
617
565
618
Tuple struct patterns match tuple struct and enum values that match all criteria defined
566
619
by its subpatterns. They are also used to [ destructure] ( #destructuring ) a tuple struct or
@@ -576,12 +629,15 @@ A tuple struct pattern is refutable when one of its subpatterns is refutable.
576
629
>
577
630
> _ TuplePatternItems_ :\
578
631
>   ;  ;   ;  ; [ _ Pattern_ ] ` , ` \
579
- >   ;  ; | [ _ Pattern _ ] & nbsp ; ( ` , ` [ _ Pattern _ ] )< sup >+</ sup > ` , ` < sup >?</ sup > \
580
- >   ;  ; | ( [ _ Pattern_ ] ` , ` )< sup > \* </ sup > ` .. ` (` , ` [ _ Pattern_ ] )<sup >* </sup > ` , ` <sup >?</sup >
632
+ >   ;  ; | [ _ RestPattern _ ] \
633
+ >   ;  ; | [ _ Pattern_ ] & nbsp ; (` , ` [ _ Pattern_ ] )<sup >+ </sup > ` , ` <sup >?</sup >
581
634
582
635
Tuple patterns match tuple values that match all criteria defined by its subpatterns.
583
636
They are also used to [ destructure] ( #destructuring ) a tuple.
584
637
638
+ The form ` (..) ` with a single [ _ RestPattern_ ] is a special form that does not
639
+ require a comma, and matches a tuple of any size.
640
+
585
641
This pattern is refutable when one of its subpatterns is refutable.
586
642
587
643
## Grouped patterns
@@ -607,7 +663,10 @@ match int_reference {
607
663
608
664
> ** <sup >Syntax</sup >** \
609
665
> _ SlicePattern_ :\
610
- >   ;  ; ` [ ` [ _ Pattern_ ] \( ` , ` [ _ Pattern_ ] )<sup >\* </sup > ` , ` <sup >?</sup > ` ] `
666
+ >   ;  ; ` [ ` _ SlicePatternItems_ <sup >?</sup > ` ] `
667
+ >
668
+ > _ SlicePatternItems_ :\
669
+ >   ;  ; [ _ Pattern_ ] \( ` , ` [ _ Pattern_ ] )<sup >\* </sup > ` , ` <sup >?</sup >
611
670
612
671
Slice patterns can match both arrays of fixed size and slices of dynamic size.
613
672
``` rust
@@ -628,6 +687,11 @@ match v[..] {
628
687
};
629
688
```
630
689
690
+ Slice patterns are irrefutable when matching an array as long as each element
691
+ is irrefutable. When matching a slice, it is irrefutable only in the form with
692
+ a single ` .. ` [ rest pattern] ( #rest-patterns ) or [ identifier
693
+ pattern] ( #identifier-patterns ) with the ` .. ` rest pattern as a subpattern.
694
+
631
695
## Path patterns
632
696
633
697
> ** <sup >Syntax</sup >** \
@@ -664,6 +728,7 @@ refer to refutable constants or enum variants for enums with multiple variants.
664
728
[ _QualifiedPathInExpression_ ] : paths.md#qualified-paths
665
729
[ _RangePattern_ ] : #range-patterns
666
730
[ _ReferencePattern_ ] : #reference-patterns
731
+ [ _RestPattern_ ] : #rest-patterns
667
732
[ _SlicePattern_ ] : #slice-patterns
668
733
[ _StructPattern_ ] : #struct-patterns
669
734
[ _TuplePattern_ ] : #tuple-patterns
0 commit comments