Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 19 additions & 4 deletions src/expressions/range-expr.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
>    | _RangeFromExpr_
>    | _RangeToExpr_
>    | _RangeFullExpr_
>    | _RangeInclusiveExpr_
>    | _RangeToInclusiveExpr_
>
> _RangeExpr_ :
>    [_Expression_] `..` [_Expression_]
Expand All @@ -18,6 +20,12 @@
>
> _RangeFullExpr_ :
>    `..`
>
> _RangeInclusiveExpr_ :
>    [_Expression_] `..=` [_Expression_]
>
> _RangeToInclusiveExpr_ :
>    `..=` [_Expression_]

The `..` operator will construct an object of one of the `std::ops::Range` (or
`core::ops::Range`) variants, according to the following table:
Expand All @@ -28,14 +36,18 @@ The `..` operator will construct an object of one of the `std::ops::Range` (or
| _RangeFromExpr_ | start`..` | [std::ops::RangeFrom] | start ≤ x |
| _RangeToExpr_ | `..`end | [std::ops::RangeTo] | x < end |
| _RangeFullExpr_ | `..` | [std::ops::RangeFull] | - |
| _RangeInclusiveExpr_ | start`..=`end | [std::ops::RangeInclusive] | start ≤ x ≤ end |
| _RangeToInclusiveExpr_ | `..=`end | [std::ops::RangeToInclusive] | x ≤ end |

Examples:

```rust
1..2; // std::ops::Range
3..; // std::ops::RangeFrom
..4; // std::ops::RangeTo
..; // std::ops::RangeFull
let a = 1..2; // std::ops::Range
let a = 3..; // std::ops::RangeFrom
let a = ..4; // std::ops::RangeTo
let a = ..; // std::ops::RangeFull
let a = 1..=2; // std::ops::RangeInclusive
let a = ..=10; // std::ops::RangeToInclusive
```

The following expressions are equivalent.
Expand All @@ -61,3 +73,6 @@ for i in 1..11 {
[std::ops::RangeFrom]: https://doc.rust-lang.org/std/ops/struct.RangeFrom.html
[std::ops::RangeTo]: https://doc.rust-lang.org/std/ops/struct.RangeTo.html
[std::ops::RangeFull]: https://doc.rust-lang.org/std/ops/struct.RangeFull.html
[std::ops::RangeInclusive]: https://doc.rust-lang.org/std/ops/struct.RangeInclusive.html
[std::ops::RangeToInclusive]: https://doc.rust-lang.org/std/ops/struct.RangeToInclusive.html