Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Document RangeFrom patterns #900

Merged
merged 5 commits into from
Sep 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 32 additions & 6 deletions src/patterns.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
>    | [_IdentifierPattern_]\
>    | [_WildcardPattern_]\
>    | [_RestPattern_]\
>    | [_ObsoleteRangePattern_]\
>    | [_ReferencePattern_]\
>    | [_StructPattern_]\
>    | [_TupleStructPattern_]\
Expand Down Expand Up @@ -401,7 +400,15 @@ match tuple {

> **<sup>Syntax</sup>**\
> _RangePattern_ :\
> &nbsp;&nbsp; _RangePatternBound_ `..=` _RangePatternBound_
> &nbsp;&nbsp; &nbsp;&nbsp; _InclusiveRangePattern_\
> &nbsp;&nbsp; | _HalfOpenRangePattern_\
> &nbsp;&nbsp; | _ObsoleteRangePattern_
>
> _InclusiveRangePattern_ :\
> &nbsp;&nbsp; &nbsp;&nbsp; _RangePatternBound_ `..=` _RangePatternBound_
>
> _HalfOpenRangePattern_ :\
> &nbsp;&nbsp; | _RangePatternBound_ `..`
>
> _ObsoleteRangePattern_ :\
> &nbsp;&nbsp; _RangePatternBound_ `...` _RangePatternBound_
Expand All @@ -414,11 +421,20 @@ match tuple {
> &nbsp;&nbsp; | [_PathInExpression_]\
> &nbsp;&nbsp; | [_QualifiedPathInExpression_]

Range patterns match values that are within the closed range defined by its lower and
upper bounds. For example, a pattern `'m'..='p'` will match only the values `'m'`, `'n'`,
`'o'`, and `'p'`. The bounds can be literals or paths that point to constant values.
Range patterns match values within the range defined by their bounds. A range pattern may be
closed or half-open. A range pattern is closed if it has both a lower and an upper bound, and
it matches all the values between and including both of its bounds. A range pattern that is
half-open is written with a lower bound but not an upper bound, and matches any value equal to
or greater than the specified lower bound.

For example, a pattern `'m'..='p'` will match only the values `'m'`, `'n'`, `'o'`, and `'p'`. For an integer the
pattern `1..` will match 9, or 9001, or 9007199254740991 (if it is of an appropriate size), but
not 0, and not negative numbers for signed integers. The bounds can be literals or paths that point
to constant values.

A half-open range pattern in the style `a..` cannot be used to match within the context of a slice.

A pattern a `..=` b must always have a &le; b. It is an error to have a range pattern
A pattern `a..=b` must always have a &le; b. It is an error to have a range pattern
`10..=0`, for example.

The `...` syntax is kept for backwards compatibility.
Expand Down Expand Up @@ -450,6 +466,12 @@ println!("{}", match ph {
_ => unreachable!(),
});

# let uint: u32 = 5;
match uint {
0 => "zero!",
1.. => "positive number!",
};

// using paths to constants:
# const TROPOSPHERE_MIN : u8 = 6;
# const TROPOSPHERE_MAX : u8 = 20;
Expand Down Expand Up @@ -720,6 +742,10 @@ is irrefutable. When matching a slice, it is irrefutable only in the form with
a single `..` [rest pattern](#rest-patterns) or [identifier
pattern](#identifier-patterns) with the `..` rest pattern as a subpattern.

Within a slice, a half-open range pattern like `a..` must be enclosed in parentheses,
as in `(a..)`, to clarify it is intended to match a single value.
A future version of Rust may give the non-parenthesized version an alternate meaning.

## Path patterns

> **<sup>Syntax</sup>**\
Expand Down
3 changes: 2 additions & 1 deletion src/tokens.md
Original file line number Diff line number Diff line change
Expand Up @@ -586,7 +586,7 @@ usages and meanings are defined in the linked pages.
| `@` | At | [Subpattern binding]
| `_` | Underscore | [Wildcard patterns], [Inferred types], Unnamed items in [constants], [extern crates], and [use declarations]
| `.` | Dot | [Field access][field], [Tuple index]
| `..` | DotDot | [Range][range], [Struct expressions], [Patterns]
| `..` | DotDot | [Range][range], [Struct expressions], [Patterns], [Range Patterns][rangepat]
| `...` | DotDotDot | [Variadic functions][extern], [Range patterns]
| `..=` | DotDotEq | [Inclusive Range][range], [Range patterns]
| `,` | Comma | Various separators
Expand Down Expand Up @@ -646,6 +646,7 @@ them are referred to as "token trees" in [macros]. The three types of brackets
[patterns]: patterns.md
[question]: expressions/operator-expr.md#the-question-mark-operator
[range]: expressions/range-expr.md
[rangepat]: patterns.md#range-patterns
[raw pointers]: types/pointer.md#raw-pointers-const-and-mut
[references]: types/pointer.md
[sized]: trait-bounds.md#sized
Expand Down