Skip to content

Commit e844f95

Browse files
committed
Eliminate 'half open' terminology from range pattern grammar
As described in issue 1329, the Reference's current use of this terminology does not match rustc's use. Following the core::ops trait terminology (as is already done for RangeExpression) results in a clearer grammar.
1 parent b9ccb09 commit e844f95

File tree

1 file changed

+21
-24
lines changed

1 file changed

+21
-24
lines changed

src/patterns.md

+21-24
Original file line numberDiff line numberDiff line change
@@ -380,16 +380,19 @@ match tuple {
380380

381381
> **<sup>Syntax</sup>**\
382382
> _RangePattern_ :\
383-
> &nbsp;&nbsp; &nbsp;&nbsp; _InclusiveRangePattern_\
384-
> &nbsp;&nbsp; | _HalfOpenRangePattern_\
383+
> &nbsp;&nbsp; &nbsp;&nbsp; _RangeInclusivePattern_\
384+
> &nbsp;&nbsp; | _RangeFromPattern_\
385+
> &nbsp;&nbsp; | _RangeToInclusivePattern_\
385386
> &nbsp;&nbsp; | _ObsoleteRangePattern_
386387
>
387-
> _InclusiveRangePattern_ :\
388+
> _RangeInclusivePattern_ :\
388389
> &nbsp;&nbsp; &nbsp;&nbsp; _RangePatternBound_ `..=` _RangePatternBound_
389390
>
390-
> _HalfOpenRangePattern_ :\
391+
> _RangeFromPattern_ :\
391392
> &nbsp;&nbsp; &nbsp;&nbsp; _RangePatternBound_ `..`
392-
> &nbsp;&nbsp; | `..=` _RangePatternBound_
393+
>
394+
> _RangeToInclusivePattern_ :\
395+
> &nbsp;&nbsp; &nbsp;&nbsp; `..=` _RangePatternBound_
393396
>
394397
> _ObsoleteRangePattern_ :\
395398
> &nbsp;&nbsp; _RangePatternBound_ `...` _RangePatternBound_
@@ -402,36 +405,29 @@ match tuple {
402405
> &nbsp;&nbsp; | [_PathExpression_]
403406
404407
*Range patterns* match scalar values within the range defined by their bounds.
405-
A bound on the left of its sigils is a *lower bound*.
408+
They comprise a *sigil* (one of `..`, `..=`, or `...`) and a bound on one or both sides.
409+
A bound on the left of the sigil is a *lower bound*.
406410
A bound on the right is an *upper bound*.
407-
A range pattern may be closed or half-open.
408-
409-
A range pattern is *closed* if it has both a lower and an upper bound.
410-
The only closed ranged pattern is the inclusive range pattern.
411411

412-
*Inclusive range patterns* match all the values between and including both of its bounds.
413-
It is written as its lower bounds, followed by `..=`, followed by its upper bounds.
414-
The type of it is the type unification of its upper and lower bounds.
412+
A range pattern with both a lower and upper bound will match all values between and including both of its bounds.
413+
It is written as its lower bound, followed by `..=`, followed by its upper bound.
414+
The type of the range pattern is the type unification of its upper and lower bounds.
415415

416416
For example, a pattern `'m'..='p'` will match only the values `'m'`, `'n'`, `'o'`, and `'p'`.
417417

418418
The lower bound cannot be greater than the upper bound.
419419
That is, in `a..=b`, a &le; b must be the case.
420420
For example, it is an error to have a range pattern `10..=0`.
421421

422-
Range patterns are *half-open* if they have only an upper or lower bound.
423-
They have the same type as their upper or lower bound.
424-
425-
A half open range with only a lower bound is written as its lower bound followed by `..`.
426-
These range patterns will match on any value greater than or equal to the lower bound.
422+
A range pattern with only a lower bound will match any value greater than or equal to the lower bound.
423+
It is written as its lower bound followed by `..`, and has the same type as its lower bound.
427424
For example, `1..` will match 1, 9, or 9001, or 9007199254740991 (if it is of an appropriate size), but not 0, and not negative numbers for signed integers.
428-
The bounds can be literals or paths that point to constant values.
429425

430-
A half open range with only an upper bound is written as `..=` followed by its upper bound.
431-
These range patterns will match on any value less than or equal to the upper bound.
426+
A range pattern with only an upper bound matches any value less than or equal to the upper bound.
427+
It is written as `..=` followed by its upper bound, and has the same type as its upper bound.
432428
For example, `..=10` will match 10, 1, 0, and for signed integer types, all negative values.
433429

434-
Half-open range patterns cannot be used as the top-level pattern for subpatterns in [slice patterns](#slice-patterns).
430+
Range patterns with only one bound cannot be used as the top-level pattern for subpatterns in [slice patterns](#slice-patterns).
435431

436432
The bounds is written as one of:
437433

@@ -529,7 +525,7 @@ The range of values for a `char` type are precisely those ranges containing all
529525
Floating point range patterns are deprecated and may be removed in a future Rust release.
530526
See [issue #41620](https://github.com/rust-lang/rust/issues/41620) for more information.
531527

532-
> **Edition Differences**: Before the 2021 edition, closed range patterns may also be written using `...` as an alternative to `..=`, with the same meaning.
528+
> **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.
533529
534530
> **Note**: Although range patterns use the same syntax as [range expressions], there are no exclusive range patterns.
535531
> That is, neither `x .. y` nor `.. x` are valid range patterns.
@@ -747,7 +743,8 @@ match v[..] {
747743
Slice patterns are irrefutable when matching an array as long as each element is irrefutable.
748744
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.
749745

750-
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.
746+
Within a slice, a range pattern without both lower and upper bound must be enclosed in parentheses, as in `(a..)`, to clarify it is intended to match against a single slice element.
747+
A range pattern with both lower and upper bound, like `a..=b`, is not required to be enclosed in parentheses.
751748

752749
## Path patterns
753750

0 commit comments

Comments
 (0)