Skip to content

Commit 15a8a29

Browse files
committed
document inclusive range syntax
1 parent e10614a commit 15a8a29

File tree

3 files changed

+31
-1
lines changed

3 files changed

+31
-1
lines changed

src/doc/book/iterators.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ Now that you know more Rust, we can talk in detail about how this works.
1414
Ranges (the `0..10`) are 'iterators'. An iterator is something that we can
1515
call the `.next()` method on repeatedly, and it gives us a sequence of things.
1616

17+
(By the way, a range with two dots like `0..10` is inclusive on the left (so it
18+
starts at 0) and exclusive on the right (so it ends at 9). A mathematician
19+
would write "[0, 10)". To get a range that goes all the way up to 10 you can
20+
write `0...10`.)
21+
1722
Like this:
1823

1924
```rust

src/doc/book/syntax-index.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,8 @@
6666
* `..` (`..`, `expr..`, `..expr`, `expr..expr`): right-exclusive range literal.
6767
* `..` (`..expr`): struct literal update syntax. See [Structs (Update syntax)].
6868
* `..` (`variant(x, ..)`, `struct_type { x, .. }`): "and the rest" pattern binding. See [Patterns (Ignoring bindings)].
69-
* `...` (`expr ... expr`): inclusive range pattern. See [Patterns (Ranges)].
69+
* `...` (`...expr`, `expr...expr`) *in an expression*: inclusive range expression. See [Iterators].
70+
* `...` (`expr...expr`) *in a pattern*: inclusive range pattern. See [Patterns (Ranges)].
7071
* `/` (`expr / expr`): arithmetic division. Overloadable (`Div`).
7172
* `/=` (`var /= expr`): arithmetic division & assignment.
7273
* `:` (`pat: type`, `ident: type`): constraints. See [Variable Bindings], [Functions], [Structs], [Traits].
@@ -205,6 +206,7 @@
205206
[Functions (Early Returns)]: functions.html#early-returns
206207
[Functions]: functions.html
207208
[Generics]: generics.html
209+
[Iterators]: iterators.html
208210
[Lifetimes]: lifetimes.html
209211
[Loops (`for`)]: loops.html#for
210212
[Loops (`loop`)]: loops.html#loop

src/doc/reference.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2279,6 +2279,10 @@ The currently implemented features of the reference compiler are:
22792279
`#[derive_Foo] #[derive_Bar]`, which can be user-defined syntax
22802280
extensions.
22812281

2282+
* `inclusive_range_syntax` - Allows use of the `a...b` and `...b` syntax for inclusive ranges.
2283+
2284+
* `inclusive_range` - Allows use of the types that represent desugared inclusive ranges.
2285+
22822286
* `intrinsics` - Allows use of the "rust-intrinsics" ABI. Compiler intrinsics
22832287
are inherently unstable and no promise about them is made.
22842288

@@ -2750,6 +2754,25 @@ let y = 0..10;
27502754
assert_eq!(x, y);
27512755
```
27522756

2757+
Similarly, the `...` operator will construct an object of one of the
2758+
`std::ops::RangeInclusive` variants.
2759+
2760+
```
2761+
# #![feature(inclusive_range_syntax)]
2762+
1...2; // std::ops::RangeInclusive
2763+
...4; // std::ops::RangeToInclusive
2764+
```
2765+
2766+
The following expressions are equivalent.
2767+
2768+
```
2769+
# #![feature(inclusive_range_syntax, inclusive_range)]
2770+
let x = std::ops::RangeInclusive::NonEmpty {start: 0, end: 10};
2771+
let y = 0...10;
2772+
2773+
assert_eq!(x, y);
2774+
```
2775+
27532776
### Unary operator expressions
27542777

27552778
Rust defines the following unary operators. They are all written as prefix operators,

0 commit comments

Comments
 (0)