Skip to content

Commit

Permalink
Merge pull request #1484 from MultisampledNight/excl-range-patterns
Browse files Browse the repository at this point in the history
patterns: include yet unstable exclusive range patterns
  • Loading branch information
ehuss authored May 7, 2024
2 parents 5181795 + 95ab920 commit 5e8b3f6
Showing 1 changed file with 6 additions and 5 deletions.
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

0 comments on commit 5e8b3f6

Please sign in to comment.