Skip to content
Merged
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
45 changes: 44 additions & 1 deletion src/expressions/range-expr.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,35 @@
# Range expressions

> **<sup>Syntax</sup>**
> _RangeExpression_ :
> &nbsp;&nbsp; &nbsp;&nbsp; _RangeExpr_
> &nbsp;&nbsp; | _RangeFromExpr_
> &nbsp;&nbsp; | _RangeToExpr_
> &nbsp;&nbsp; | _RangeFullExpr_
>
> _RangeExpr_ :
> &nbsp;&nbsp; [_Expression_] `..` [_Expression_]
>
> _RangeFromExpr_ :
> &nbsp;&nbsp; [_Expression_] `..`
>
> _RangeToExpr_ :
> &nbsp;&nbsp; `..` [_Expression_]
>
> _RangeFullExpr_ :
> &nbsp;&nbsp; `..`

The `..` operator will construct an object of one of the `std::ops::Range` (or
`core::ops::Range`) variants.
`core::ops::Range`) variants, according to the following table:

| Production | Syntax | Type | Range |
|------------------------|---------------|------------------------------|-----------------------|
| _RangeExpr_ | start`..`end | [std::ops::Range] | start &le; x &lt; end |
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be worth using <pre> tags to preserve the formatting in the Range column?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mean to keep the ≤, x and < aligned (like they are in the source code)?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes.

| _RangeFromExpr_ | start`..` | [std::ops::RangeFrom] | start &le; x |
| _RangeToExpr_ | `..`end | [std::ops::RangeTo] | x &lt; end |
| _RangeFullExpr_ | `..` | [std::ops::RangeFull] | - |

Examples:

```rust
1..2; // std::ops::Range
Expand All @@ -18,3 +46,18 @@ let y = 0..10;

assert_eq!(x, y);
```

Ranges can be used in `for` loops:

```rust
for i in 1..11 {
println!("{}", i);
}
```

[_Expression_]: expressions.html

[std::ops::Range]: https://doc.rust-lang.org/std/ops/struct.Range.html
[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