1# Range expressions
2
3> **<sup>Syntax</sup>**\
4> _RangeExpression_ :\
5> &nbsp;&nbsp; &nbsp;&nbsp; _RangeExpr_\
6> &nbsp;&nbsp; | _RangeFromExpr_\
7> &nbsp;&nbsp; | _RangeToExpr_\
8> &nbsp;&nbsp; | _RangeFullExpr_\
9> &nbsp;&nbsp; | _RangeInclusiveExpr_\
10> &nbsp;&nbsp; | _RangeToInclusiveExpr_
11>
12> _RangeExpr_ :\
13> &nbsp;&nbsp; [_Expression_] `..` [_Expression_]
14>
15> _RangeFromExpr_ :\
16> &nbsp;&nbsp; [_Expression_] `..`
17>
18> _RangeToExpr_ :\
19> &nbsp;&nbsp; `..` [_Expression_]
20>
21> _RangeFullExpr_ :\
22> &nbsp;&nbsp; `..`
23>
24> _RangeInclusiveExpr_ :\
25> &nbsp;&nbsp; [_Expression_] `..=` [_Expression_]
26>
27> _RangeToInclusiveExpr_ :\
28> &nbsp;&nbsp; `..=` [_Expression_]
29
30The `..` and `..=` operators will construct an object of one of the `std::ops::Range` (or `core::ops::Range`) variants, according to the following table:
31
32| Production             | Syntax        | Type                         | Range                 |
33|------------------------|---------------|------------------------------|-----------------------|
34| _RangeExpr_            | start`..`end  | [std::ops::Range]            | start &le; x &lt; end |
35| _RangeFromExpr_        | start`..`     | [std::ops::RangeFrom]        | start &le; x          |
36| _RangeToExpr_          | `..`end       | [std::ops::RangeTo]          |            x &lt; end |
37| _RangeFullExpr_        | `..`          | [std::ops::RangeFull]        |            -          |
38| _RangeInclusiveExpr_   | start`..=`end | [std::ops::RangeInclusive]   | start &le; x &le; end |
39| _RangeToInclusiveExpr_ | `..=`end      | [std::ops::RangeToInclusive] |            x &le; end |
40
41Examples:
42
43```rust
441..2;   // std::ops::Range
453..;    // std::ops::RangeFrom
46..4;    // std::ops::RangeTo
47..;     // std::ops::RangeFull
485..=6;  // std::ops::RangeInclusive
49..=7;   // std::ops::RangeToInclusive
50```
51
52The following expressions are equivalent.
53
54```rust
55let x = std::ops::Range {start: 0, end: 10};
56let y = 0..10;
57
58assert_eq!(x, y);
59```
60
61Ranges can be used in `for` loops:
62
63```rust
64for i in 1..11 {
65    println!("{}", i);
66}
67```
68
69[_Expression_]: ../expressions.md
70
71[std::ops::Range]: https://doc.rust-lang.org/std/ops/struct.Range.html
72[std::ops::RangeFrom]: https://doc.rust-lang.org/std/ops/struct.RangeFrom.html
73[std::ops::RangeTo]: https://doc.rust-lang.org/std/ops/struct.RangeTo.html
74[std::ops::RangeFull]: https://doc.rust-lang.org/std/ops/struct.RangeFull.html
75[std::ops::RangeInclusive]: https://doc.rust-lang.org/std/ops/struct.RangeInclusive.html
76[std::ops::RangeToInclusive]: https://doc.rust-lang.org/std/ops/struct.RangeToInclusive.html
77