Skip to content

Commit 2659198

Browse files
show how iterating over RangeTo and RangeToInclusive fails
Feedback on PR #35701 seems to be positive, so this does the same thing for `RangeTo` and `RangeToInclusive`.
1 parent 7ac11ca commit 2659198

File tree

1 file changed

+43
-12
lines changed

1 file changed

+43
-12
lines changed

src/libcore/ops.rs

Lines changed: 43 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1610,17 +1610,33 @@ impl<Idx: PartialOrd<Idx>> RangeFrom<Idx> {
16101610
///
16111611
/// It cannot serve as an iterator because it doesn't have a starting point.
16121612
///
1613+
/// # Examples
1614+
///
1615+
/// The `..{integer}` syntax is a `RangeTo`:
1616+
///
1617+
/// ```
1618+
/// assert_eq!((..5), std::ops::RangeTo{ end: 5 });
16131619
/// ```
1614-
/// fn main() {
1615-
/// assert_eq!((..5), std::ops::RangeTo{ end: 5 });
16161620
///
1617-
/// let arr = [0, 1, 2, 3];
1618-
/// assert_eq!(arr[ .. ], [0,1,2,3]);
1619-
/// assert_eq!(arr[ ..3], [0,1,2 ]); // RangeTo
1620-
/// assert_eq!(arr[1.. ], [ 1,2,3]);
1621-
/// assert_eq!(arr[1..3], [ 1,2 ]);
1621+
/// It does not have an `IntoIterator` implementation, so you can't use it in a
1622+
/// `for` loop directly. This won't compile:
1623+
///
1624+
/// ```ignore
1625+
/// for i in ..5 {
1626+
/// // ...
16221627
/// }
16231628
/// ```
1629+
///
1630+
/// When used as a slicing index, `RangeTo` produces a slice of all array
1631+
/// elements before the index indicated by `end`.
1632+
///
1633+
/// ```
1634+
/// let arr = [0, 1, 2, 3];
1635+
/// assert_eq!(arr[ .. ], [0,1,2,3]);
1636+
/// assert_eq!(arr[ ..3], [0,1,2 ]); // RangeTo
1637+
/// assert_eq!(arr[1.. ], [ 1,2,3]);
1638+
/// assert_eq!(arr[1..3], [ 1,2 ]);
1639+
/// ```
16241640
#[derive(Copy, Clone, PartialEq, Eq, Hash)]
16251641
#[stable(feature = "rust1", since = "1.0.0")]
16261642
pub struct RangeTo<Idx> {
@@ -1748,16 +1764,31 @@ impl<Idx: PartialOrd<Idx>> RangeInclusive<Idx> {
17481764
///
17491765
/// # Examples
17501766
///
1767+
/// The `...{integer}` syntax is a `RangeToInclusive`:
1768+
///
17511769
/// ```
17521770
/// #![feature(inclusive_range,inclusive_range_syntax)]
1753-
/// fn main() {
1754-
/// assert_eq!((...5), std::ops::RangeToInclusive{ end: 5 });
1771+
/// assert_eq!((...5), std::ops::RangeToInclusive{ end: 5 });
1772+
/// ```
17551773
///
1756-
/// let arr = [0, 1, 2, 3];
1757-
/// assert_eq!(arr[ ...2], [0,1,2 ]); // RangeToInclusive
1758-
/// assert_eq!(arr[1...2], [ 1,2 ]);
1774+
/// It does not have an `IntoIterator` implementation, so you can't use it in a
1775+
/// `for` loop directly. This won't compile:
1776+
///
1777+
/// ```ignore
1778+
/// for i in ...5 {
1779+
/// // ...
17591780
/// }
17601781
/// ```
1782+
///
1783+
/// When used as a slicing index, `RangeToInclusive` produces a slice of all
1784+
/// array elements up to and including the index indicated by `end`.
1785+
///
1786+
/// ```
1787+
/// #![feature(inclusive_range_syntax)]
1788+
/// let arr = [0, 1, 2, 3];
1789+
/// assert_eq!(arr[ ...2], [0,1,2 ]); // RangeToInclusive
1790+
/// assert_eq!(arr[1...2], [ 1,2 ]);
1791+
/// ```
17611792
#[derive(Copy, Clone, PartialEq, Eq, Hash)]
17621793
#[unstable(feature = "inclusive_range", reason = "recently added, follows RFC", issue = "28237")]
17631794
pub struct RangeToInclusive<Idx> {

0 commit comments

Comments
 (0)