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 values within the range defined by their bounds.
404
+
*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*.
406
+
A bound on the right is an *upper bound*.
404
407
A range pattern may be closed or half-open.
405
-
A range pattern is closed if it has both a lower and an upper bound, and it matches all the values between and including both of its bounds.
406
-
A range pattern that is 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.
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
+
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.
407
415
408
416
For example, a pattern `'m'..='p'` will match only the values `'m'`, `'n'`, `'o'`, and `'p'`.
409
-
For an integer the pattern `1..` will match 9, or 9001, or 9007199254740991 (if it is of an appropriate size), but not 0, and not negative numbers for signed integers.
417
+
418
+
The lower bound cannot be greater than the upper bound.
419
+
That is, in `a..=b`, a ≤ b must be the case.
420
+
For example, it is an error to have a range pattern `10..=0`.
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.
427
+
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.
410
428
The bounds can be literals or paths that point to constant values.
411
429
412
-
A half-open range pattern in the style `a..` cannot be used to match within the context of a slice.
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.
432
+
For example, `..=10` will match 10, 1, 0, and for signed interger types, all negative values.
413
433
414
-
A pattern `a..=b` must always have a ≤ b.
415
-
It is an error to have a range pattern `10..=0`, for example.
434
+
Half-open range patterns cannot be used as the top-level pattern for subpatterns in [slice patterns](#slice-patterns).
416
435
417
-
Range patterns only work on scalar types. The accepted types are:
This is being deprecated and will not be available in a future version of Rust (see [issue #41620](https://github.com/rust-lang/rust/issues/41620)).
438
+
* A character, byte, integer, or float literal.
439
+
* A `-` followed by an integer or float literal.
440
+
* A [path]
441
+
442
+
If the bounds is written as a path, after macro resolution, the path must resolve to a constant item of the type `char`, an integer type, or a float type.
443
+
444
+
The type and value of the bounds is dependent upon how it is written out.
445
+
If the bounds is a [path], the pattern has the type and value of the [constant] the path resolves to.
446
+
If it is a literal, it has the type and value of the corresponding [literal expression].
447
+
If is a literal preceded by a `-`, it has the same type as the corresponding [literal expression] and the value of [negating] the value of the corresponding literal expression.
423
448
424
449
Examples:
425
450
@@ -496,13 +521,19 @@ println!("{}", match 0xfacade {
496
521
});
497
522
```
498
523
499
-
Range patterns for (non-`usize` and -`isize`) integer and `char` types are irrefutable when they span the entire set of possible values of a type.
524
+
Range patterns for fix-width integer and `char` types are irrefutable when they span the entire set of possible values of a type.
500
525
For example, `0u8..=255u8` is irrefutable.
501
526
The range of values for an integer type is the closed range from its minimum to maximum value.
502
527
The range of values for a `char` type are precisely those ranges containing all Unicode Scalar Values: `'\u{0000}'..='\u{D7FF}'` and `'\u{E000}'..='\u{10FFFF}'`.
503
528
529
+
Floating point range patterns are deprecated and may be removed in a future Rust release.
530
+
See [issue #41620](https://github.com/rust-lang/rust/issues/41620) for more information.
531
+
504
532
> **Edition Differences**: Before the 2021 edition, closed range patterns may also be written using `...` as an alternative to `..=`, with the same meaning.
505
533
534
+
> **Note**: Although range patterns use the same syntax as [range expressions], there are no exclusive range patterns.
535
+
> That is, neither `x .. y` nor `.. x` are valid range patterns.
536
+
506
537
## Reference patterns
507
538
508
539
> **<sup>Syntax</sup>**\
@@ -809,8 +840,13 @@ For example, `x @ A(..) | B(..)` will result in an error that `x` is not bound i
0 commit comments