13
13
>   ;  ; | [ _ IdentifierPattern_ ] \
14
14
>   ;  ; | [ _ WildcardPattern_ ] \
15
15
>   ;  ; | [ _ RestPattern_ ] \
16
- >   ;  ; | [ _ ObsoleteRangePattern_ ] \
17
16
>   ;  ; | [ _ ReferencePattern_ ] \
18
17
>   ;  ; | [ _ StructPattern_ ] \
19
18
>   ;  ; | [ _ TupleStructPattern_ ] \
@@ -407,7 +406,15 @@ match tuple {
407
406
408
407
> ** <sup >Syntax</sup >** \
409
408
> _ RangePattern_ :\
410
- >   ;  ; _ RangePatternBound_ ` ..= ` _ RangePatternBound_
409
+ >   ;  ;   ;  ; _ InclusiveRangePattern_ \
410
+ >   ;  ; | _ HalfOpenRangePattern_ \
411
+ >   ;  ; | _ ObsoleteRangePattern_
412
+ >
413
+ > _ InclusiveRangePattern_ :\
414
+ >   ;  ;   ;  ; _ RangePatternBound_ ` ..= ` _ RangePatternBound_
415
+ >
416
+ > _ HalfOpenRangePattern_ :\
417
+ >   ;  ; | _ RangePatternBound_ ` .. `
411
418
>
412
419
> _ ObsoleteRangePattern_ :\
413
420
>   ;  ; _ RangePatternBound_ ` ... ` _ RangePatternBound_
@@ -420,11 +427,20 @@ match tuple {
420
427
>   ;  ; | [ _ PathInExpression_ ] \
421
428
>   ;  ; | [ _ QualifiedPathInExpression_ ]
422
429
423
- Range patterns match values that are within the closed range defined by its lower and
424
- upper bounds. For example, a pattern ` 'm'..='p' ` will match only the values ` 'm' ` , ` 'n' ` ,
425
- ` 'o' ` , and ` 'p' ` . The bounds can be literals or paths that point to constant values.
430
+ Range patterns match values within the range defined by their bounds. A range pattern may be
431
+ closed or half-open. A range pattern is closed if it has both a lower and an upper bound, and
432
+ it matches all the values between and including both of its bounds. A range pattern that is
433
+ half-open is written with a lower bound but not an upper bound, and matches any value equal to
434
+ or greater than the specified lower bound.
435
+
436
+ For example, a pattern ` 'm'..='p' ` will match only the values ` 'm' ` , ` 'n' ` , ` 'o' ` , and ` 'p' ` . For an integer the
437
+ pattern ` 1.. ` will match 9, or 9001, or 9007199254740991 (if it is of an appropriate size), but
438
+ not 0, and not negative numbers for signed integers. The bounds can be literals or paths that point
439
+ to constant values.
440
+
441
+ A half-open range pattern in the style ` a.. ` cannot be used to match within the context of a slice.
426
442
427
- A pattern a ` ..= ` b must always have a &le ; b. It is an error to have a range pattern
443
+ A pattern ` a ..=b ` must always have a &le ; b. It is an error to have a range pattern
428
444
` 10..=0 ` , for example.
429
445
430
446
The ` ... ` syntax is kept for backwards compatibility.
@@ -456,6 +472,12 @@ println!("{}", match ph {
456
472
_ => unreachable! (),
457
473
});
458
474
475
+ # let uint : u32 = 5 ;
476
+ match uint {
477
+ 0 => " zero!" ,
478
+ 1 .. => " positive number!" ,
479
+ };
480
+
459
481
// using paths to constants:
460
482
# const TROPOSPHERE_MIN : u8 = 6 ;
461
483
# const TROPOSPHERE_MAX : u8 = 20 ;
@@ -736,6 +758,10 @@ is irrefutable. When matching a slice, it is irrefutable only in the form with
736
758
a single ` .. ` [ rest pattern] ( #rest-patterns ) or [ identifier
737
759
pattern] ( #identifier-patterns ) with the ` .. ` rest pattern as a subpattern.
738
760
761
+ Within a slice, a half-open range pattern like ` a.. ` must be enclosed in parentheses,
762
+ as in ` (a..) ` , to clarify it is intended to match a single value.
763
+ A future version of Rust may give the non-parenthesized version an alternate meaning.
764
+
739
765
## Path patterns
740
766
741
767
> ** <sup >Syntax</sup >** \
0 commit comments