You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
*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*.
406
410
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.
411
411
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.
415
415
416
416
For example, a pattern `'m'..='p'` will match only the values `'m'`, `'n'`, `'o'`, and `'p'`.
417
417
418
418
The lower bound cannot be greater than the upper bound.
419
419
That is, in `a..=b`, a ≤ b must be the case.
420
420
For example, it is an error to have a range pattern `10..=0`.
421
421
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.
427
424
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.
429
425
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.
432
428
For example, `..=10` will match 10, 1, 0, and for signed integer types, all negative values.
433
429
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).
435
431
436
432
The bounds is written as one of:
437
433
@@ -529,7 +525,7 @@ The range of values for a `char` type are precisely those ranges containing all
529
525
Floating point range patterns are deprecated and may be removed in a future Rust release.
530
526
See [issue #41620](https://github.com/rust-lang/rust/issues/41620) for more information.
531
527
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.
533
529
534
530
> **Note**: Although range patterns use the same syntax as [range expressions], there are no exclusive range patterns.
535
531
> That is, neither `x .. y` nor `.. x` are valid range patterns.
@@ -747,7 +743,8 @@ match v[..] {
747
743
Slice patterns are irrefutable when matching an array as long as each element is irrefutable.
748
744
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.
749
745
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.
0 commit comments