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