Skip to content

Commit ff45e61

Browse files
Rollup merge of rust-lang#35771 - matthew-piziak:range-inclusive-example-error, r=steveklabnik
show how iterating over `RangeTo` and `RangeToInclusive` fails Feedback on PR rust-lang#35701 seems to be positive, so this does the same thing for `RangeTo` and `RangeToInclusive`.
2 parents f60cb17 + 2659198 commit ff45e61

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
@@ -1873,17 +1873,33 @@ impl<Idx: PartialOrd<Idx>> RangeFrom<Idx> {
18731873
///
18741874
/// It cannot serve as an iterator because it doesn't have a starting point.
18751875
///
1876+
/// # Examples
1877+
///
1878+
/// The `..{integer}` syntax is a `RangeTo`:
1879+
///
1880+
/// ```
1881+
/// assert_eq!((..5), std::ops::RangeTo{ end: 5 });
18761882
/// ```
1877-
/// fn main() {
1878-
/// assert_eq!((..5), std::ops::RangeTo{ end: 5 });
18791883
///
1880-
/// let arr = [0, 1, 2, 3];
1881-
/// assert_eq!(arr[ .. ], [0,1,2,3]);
1882-
/// assert_eq!(arr[ ..3], [0,1,2 ]); // RangeTo
1883-
/// assert_eq!(arr[1.. ], [ 1,2,3]);
1884-
/// assert_eq!(arr[1..3], [ 1,2 ]);
1884+
/// It does not have an `IntoIterator` implementation, so you can't use it in a
1885+
/// `for` loop directly. This won't compile:
1886+
///
1887+
/// ```ignore
1888+
/// for i in ..5 {
1889+
/// // ...
18851890
/// }
18861891
/// ```
1892+
///
1893+
/// When used as a slicing index, `RangeTo` produces a slice of all array
1894+
/// elements before the index indicated by `end`.
1895+
///
1896+
/// ```
1897+
/// let arr = [0, 1, 2, 3];
1898+
/// assert_eq!(arr[ .. ], [0,1,2,3]);
1899+
/// assert_eq!(arr[ ..3], [0,1,2 ]); // RangeTo
1900+
/// assert_eq!(arr[1.. ], [ 1,2,3]);
1901+
/// assert_eq!(arr[1..3], [ 1,2 ]);
1902+
/// ```
18871903
#[derive(Copy, Clone, PartialEq, Eq, Hash)]
18881904
#[stable(feature = "rust1", since = "1.0.0")]
18891905
pub struct RangeTo<Idx> {
@@ -2011,16 +2027,31 @@ impl<Idx: PartialOrd<Idx>> RangeInclusive<Idx> {
20112027
///
20122028
/// # Examples
20132029
///
2030+
/// The `...{integer}` syntax is a `RangeToInclusive`:
2031+
///
20142032
/// ```
20152033
/// #![feature(inclusive_range,inclusive_range_syntax)]
2016-
/// fn main() {
2017-
/// assert_eq!((...5), std::ops::RangeToInclusive{ end: 5 });
2034+
/// assert_eq!((...5), std::ops::RangeToInclusive{ end: 5 });
2035+
/// ```
20182036
///
2019-
/// let arr = [0, 1, 2, 3];
2020-
/// assert_eq!(arr[ ...2], [0,1,2 ]); // RangeToInclusive
2021-
/// assert_eq!(arr[1...2], [ 1,2 ]);
2037+
/// It does not have an `IntoIterator` implementation, so you can't use it in a
2038+
/// `for` loop directly. This won't compile:
2039+
///
2040+
/// ```ignore
2041+
/// for i in ...5 {
2042+
/// // ...
20222043
/// }
20232044
/// ```
2045+
///
2046+
/// When used as a slicing index, `RangeToInclusive` produces a slice of all
2047+
/// array elements up to and including the index indicated by `end`.
2048+
///
2049+
/// ```
2050+
/// #![feature(inclusive_range_syntax)]
2051+
/// let arr = [0, 1, 2, 3];
2052+
/// assert_eq!(arr[ ...2], [0,1,2 ]); // RangeToInclusive
2053+
/// assert_eq!(arr[1...2], [ 1,2 ]);
2054+
/// ```
20242055
#[derive(Copy, Clone, PartialEq, Eq, Hash)]
20252056
#[unstable(feature = "inclusive_range", reason = "recently added, follows RFC", issue = "28237")]
20262057
pub struct RangeToInclusive<Idx> {

0 commit comments

Comments
 (0)