Skip to content

Commit f64fd58

Browse files
author
Alexis Hunt
committed
Document inclusive ranges.
Provides the reference documentation for rust-lang/rust#28237.
1 parent fe87bd7 commit f64fd58

File tree

2 files changed

+32
-11
lines changed

2 files changed

+32
-11
lines changed

src/expressions/match-expr.md

+11-5
Original file line numberDiff line numberDiff line change
@@ -95,18 +95,23 @@ match x {
9595
}
9696
```
9797

98-
Multiple match patterns may be joined with the `|` operator. A range of values
99-
may be specified with `...`. For example:
98+
Multiple match patterns may be joined with the `|` operator. An inclusive range
99+
of values may be specified with `..=`. For example:
100100

101101
```rust
102-
# let x = 2;
102+
# let x = 9;
103103
let message = match x {
104104
0 | 1 => "not many",
105-
2 ... 9 => "a few",
105+
2 ..= 9 => "a few",
106106
_ => "lots"
107107
};
108+
109+
assert_eq!(message, "a few");
108110
```
109111

112+
Other forms of [range] \(`..` for an exclusive range, or any range with one or
113+
both endpoints left unspecified) are not currently supported in matches.
114+
110115
Range patterns only work on scalar types (like integers and characters; not
111116
like arrays and structs, which have sub-components). A range pattern may not be
112117
a sub-range of another range pattern inside the same `match`.
@@ -128,4 +133,5 @@ let message = match maybe_digit {
128133
```
129134

130135
[place expression]: expressions.html#place-expressions-and-value-expressions
131-
[value expression]: expressions.html#place-expressions-and-value-expressions
136+
[value expression]: expressions.html#place-expressions-and-value-expressions
137+
[range]: range-expr.html

src/expressions/range-expr.md

+21-6
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
>    | _RangeFromExpr_
77
>    | _RangeToExpr_
88
>    | _RangeFullExpr_
9+
>    | _RangeInclusiveExpr_
10+
>    | _RangeToInclusiveExpr_
911
>
1012
> _RangeExpr_ :
1113
>    [_Expression_] `..` [_Expression_]
@@ -18,16 +20,25 @@
1820
>
1921
> _RangeFullExpr_ :
2022
>    `..`
23+
>
24+
> _RangeExpr_ :
25+
>    [_Expression_] `..=` [_Expression_]
26+
>
27+
> _RangeToExpr_ :
28+
>    `..=` [_Expression_]
2129
22-
The `..` operator will construct an object of one of the `std::ops::Range` (or
23-
`core::ops::Range`) variants, according to the following table:
30+
The `..` and `..=` operators will construct an object of one of the
31+
`std::ops::Range` (or `core::ops::Range`) variants, according to the following
32+
table:
2433

2534
| Production | Syntax | Type | Range |
2635
|------------------------|---------------|------------------------------|-----------------------|
2736
| _RangeExpr_ | start`..`end | [std::ops::Range] | start ≤ x < end |
2837
| _RangeFromExpr_ | start`..` | [std::ops::RangeFrom] | start ≤ x |
2938
| _RangeToExpr_ | `..`end | [std::ops::RangeTo] | x < end |
3039
| _RangeFullExpr_ | `..` | [std::ops::RangeFull] | - |
40+
| _RangeInclusiveExpr_ | start`..=`end | [std::ops::RangeInclusive] | start ≤ x ≤ end |
41+
| _RangeToInclusiveExpr_ | `..=`end | [std::ops::RangeToInclusive] | x ≤ end |
3142

3243
Examples:
3344

@@ -36,6 +47,8 @@ Examples:
3647
3..; // std::ops::RangeFrom
3748
..4; // std::ops::RangeTo
3849
..; // std::ops::RangeFull
50+
5..=6; // std::ops::RangeInclusive
51+
..=7; // std::ops::RangeToInclusive
3952
```
4053

4154
The following expressions are equivalent.
@@ -57,7 +70,9 @@ for i in 1..11 {
5770

5871
[_Expression_]: expressions.html
5972

60-
[std::ops::Range]: https://doc.rust-lang.org/std/ops/struct.Range.html
61-
[std::ops::RangeFrom]: https://doc.rust-lang.org/std/ops/struct.RangeFrom.html
62-
[std::ops::RangeTo]: https://doc.rust-lang.org/std/ops/struct.RangeTo.html
63-
[std::ops::RangeFull]: https://doc.rust-lang.org/std/ops/struct.RangeFull.html
73+
[std::ops::Range]: https://doc.rust-lang.org/std/ops/struct.Range.html
74+
[std::ops::RangeFrom]: https://doc.rust-lang.org/std/ops/struct.RangeFrom.html
75+
[std::ops::RangeTo]: https://doc.rust-lang.org/std/ops/struct.RangeTo.html
76+
[std::ops::RangeFull]: https://doc.rust-lang.org/std/ops/struct.RangeFull.html
77+
[std::ops::RangeInclusive]: https://doc.rust-lang.org/std/ops/struct.RangeInclusive.html
78+
[std::ops::RangeToInclusive]: https://doc.rust-lang.org/std/ops/struct.RangeToInclusive.html

0 commit comments

Comments
 (0)