Skip to content

Commit df5799c

Browse files
authored
Merge pull request #900 from workingjubilee/rangefrom-pat
Document RangeFrom patterns
2 parents 0e5ed7a + 76ed5db commit df5799c

File tree

2 files changed

+34
-7
lines changed

2 files changed

+34
-7
lines changed

src/patterns.md

+32-6
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
>    | [_IdentifierPattern_]\
1414
>    | [_WildcardPattern_]\
1515
>    | [_RestPattern_]\
16-
>    | [_ObsoleteRangePattern_]\
1716
>    | [_ReferencePattern_]\
1817
>    | [_StructPattern_]\
1918
>    | [_TupleStructPattern_]\
@@ -407,7 +406,15 @@ match tuple {
407406

408407
> **<sup>Syntax</sup>**\
409408
> _RangePattern_ :\
410-
> &nbsp;&nbsp; _RangePatternBound_ `..=` _RangePatternBound_
409+
> &nbsp;&nbsp; &nbsp;&nbsp; _InclusiveRangePattern_\
410+
> &nbsp;&nbsp; | _HalfOpenRangePattern_\
411+
> &nbsp;&nbsp; | _ObsoleteRangePattern_
412+
>
413+
> _InclusiveRangePattern_ :\
414+
> &nbsp;&nbsp; &nbsp;&nbsp; _RangePatternBound_ `..=` _RangePatternBound_
415+
>
416+
> _HalfOpenRangePattern_ :\
417+
> &nbsp;&nbsp; | _RangePatternBound_ `..`
411418
>
412419
> _ObsoleteRangePattern_ :\
413420
> &nbsp;&nbsp; _RangePatternBound_ `...` _RangePatternBound_
@@ -420,11 +427,20 @@ match tuple {
420427
> &nbsp;&nbsp; | [_PathInExpression_]\
421428
> &nbsp;&nbsp; | [_QualifiedPathInExpression_]
422429
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.
426442

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
428444
`10..=0`, for example.
429445

430446
The `...` syntax is kept for backwards compatibility.
@@ -456,6 +472,12 @@ println!("{}", match ph {
456472
_ => unreachable!(),
457473
});
458474

475+
# let uint: u32 = 5;
476+
match uint {
477+
0 => "zero!",
478+
1.. => "positive number!",
479+
};
480+
459481
// using paths to constants:
460482
# const TROPOSPHERE_MIN : u8 = 6;
461483
# const TROPOSPHERE_MAX : u8 = 20;
@@ -736,6 +758,10 @@ is irrefutable. When matching a slice, it is irrefutable only in the form with
736758
a single `..` [rest pattern](#rest-patterns) or [identifier
737759
pattern](#identifier-patterns) with the `..` rest pattern as a subpattern.
738760

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+
739765
## Path patterns
740766

741767
> **<sup>Syntax</sup>**\

src/tokens.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -585,7 +585,7 @@ usages and meanings are defined in the linked pages.
585585
| `@` | At | [Subpattern binding]
586586
| `_` | Underscore | [Wildcard patterns], [Inferred types], Unnamed items in [constants], [extern crates], and [use declarations]
587587
| `.` | Dot | [Field access][field], [Tuple index]
588-
| `..` | DotDot | [Range][range], [Struct expressions], [Patterns]
588+
| `..` | DotDot | [Range][range], [Struct expressions], [Patterns], [Range Patterns][rangepat]
589589
| `...` | DotDotDot | [Variadic functions][extern], [Range patterns]
590590
| `..=` | DotDotEq | [Inclusive Range][range], [Range patterns]
591591
| `,` | Comma | Various separators
@@ -646,6 +646,7 @@ them are referred to as "token trees" in [macros]. The three types of brackets
646646
[patterns]: patterns.md
647647
[question]: expressions/operator-expr.md#the-question-mark-operator
648648
[range]: expressions/range-expr.md
649+
[rangepat]: patterns.md#range-patterns
649650
[raw pointers]: types/pointer.md#raw-pointers-const-and-mut
650651
[references]: types/pointer.md
651652
[sized]: trait-bounds.md#sized

0 commit comments

Comments
 (0)