Skip to content

Document RangeFrom patterns #900

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

Merged
merged 5 commits into from
Sep 17, 2021
Merged
Changes from 1 commit
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
Prev Previous commit
Update with slice pattern restrictions
  • Loading branch information
workingjubilee committed Sep 16, 2021
commit 76ed5db1e1783e8ab2642e6c69f596381126052d
22 changes: 17 additions & 5 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,14 @@ match tuple {

> **<sup>Syntax</sup>**\
> _RangePattern_ :\
> &nbsp;&nbsp; &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_ :\
Expand All @@ -421,12 +427,14 @@ it matches all the values between and including both of its bounds. A range patt
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'`. The
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 or negative numbers for signed integers. The bounds can be literals or paths that point
not 0, and not negative numbers for signed integers. The bounds can be literals or paths that point
to constant values.

A pattern a `..=` b must always have a &le; b. It is an error to have a range pattern
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
`10..=0`, for example.

The `...` syntax is kept for backwards compatibility.
Expand Down Expand Up @@ -734,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