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

patterns: include yet unstable exclusive range patterns #1484

Merged
merged 1 commit into from
May 7, 2024
Merged
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
11 changes: 6 additions & 5 deletions src/patterns.md
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,9 @@ match tuple {
>    | _RangeToInclusivePattern_\
>    | _ObsoleteRangePattern_
>
> _RangeExclusivePattern_ :\
>       _RangePatternBound_ `..` _RangePatternBound_
>
> _RangeInclusivePattern_ :\
>       _RangePatternBound_ `..=` _RangePatternBound_
>
Expand All @@ -422,10 +425,11 @@ A bound on the left of the sigil is a *lower bound*.
A bound on the right is an *upper bound*.

A range pattern with both a lower and upper bound will match all values between and including both of its bounds.
It is written as its lower bound, followed by `..=`, followed by its upper bound.
It is written as its lower bound, followed by `..` for end-exclusive or `..=` for end-inclusive, followed by its upper bound.
The type of the range pattern is the type unification of its upper and lower bounds.

For example, a pattern `'m'..='p'` will match only the values `'m'`, `'n'`, `'o'`, and `'p'`.
Similarly, `'m'..'p'` will match only `'m'`, `'n'` and `'o'`, specifically **not** including `'p'`.

The lower bound cannot be greater than the upper bound.
That is, in `a..=b`, a ≤ b must be the case.
Expand Down Expand Up @@ -467,7 +471,7 @@ let valid_variable = match c {

# let ph = 10;
println!("{}", match ph {
0..=6 => "acid",
0..7 => "acid",
7 => "neutral",
8..=14 => "base",
_ => unreachable!(),
Expand Down Expand Up @@ -539,9 +543,6 @@ See [issue #41620](https://github.com/rust-lang/rust/issues/41620) for more info

> **Edition Differences**: Before the 2021 edition, range patterns with both a lower and upper bound may also be written using `...` in place of `..=`, with the same meaning.

> **Note**: Although range patterns use the same syntax as [range expressions], there are no exclusive range patterns.
> That is, neither `x .. y` nor `.. x` are valid range patterns.

## Reference patterns

> **<sup>Syntax</sup>**\
Expand Down