diff options
| author | Guillaume Gomez <guillaume1.gomez@gmail.com> | 2016-08-30 10:39:05 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2016-08-30 10:39:05 +0200 |
| commit | ff45e6195be10e83092e3f9bafebb305c30bbb21 (patch) | |
| tree | 5d9928c3694ff6af9609a72cdb935406814a5e4a | |
| parent | f60cb177aa147acebb9cd2fd97a661021b736490 (diff) | |
| parent | 26591986032abbad0490a45da6ba7252178c1c82 (diff) | |
Rollup merge of #35771 - matthew-piziak:range-inclusive-example-error, r=steveklabnik
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`.
| -rw-r--r-- | src/libcore/ops.rs | 55 |
1 files changed, 43 insertions, 12 deletions
diff --git a/src/libcore/ops.rs b/src/libcore/ops.rs index b9adaf0206d..61ec682c071 100644 --- a/src/libcore/ops.rs +++ b/src/libcore/ops.rs @@ -1873,17 +1873,33 @@ impl<Idx: PartialOrd<Idx>> RangeFrom<Idx> { /// /// It cannot serve as an iterator because it doesn't have a starting point. /// +/// # Examples +/// +/// The `..{integer}` syntax is a `RangeTo`: +/// +/// ``` +/// assert_eq!((..5), std::ops::RangeTo{ end: 5 }); /// ``` -/// fn main() { -/// assert_eq!((..5), std::ops::RangeTo{ end: 5 }); /// -/// let arr = [0, 1, 2, 3]; -/// assert_eq!(arr[ .. ], [0,1,2,3]); -/// assert_eq!(arr[ ..3], [0,1,2 ]); // RangeTo -/// assert_eq!(arr[1.. ], [ 1,2,3]); -/// assert_eq!(arr[1..3], [ 1,2 ]); +/// It does not have an `IntoIterator` implementation, so you can't use it in a +/// `for` loop directly. This won't compile: +/// +/// ```ignore +/// for i in ..5 { +/// // ... /// } /// ``` +/// +/// When used as a slicing index, `RangeTo` produces a slice of all array +/// elements before the index indicated by `end`. +/// +/// ``` +/// let arr = [0, 1, 2, 3]; +/// assert_eq!(arr[ .. ], [0,1,2,3]); +/// assert_eq!(arr[ ..3], [0,1,2 ]); // RangeTo +/// assert_eq!(arr[1.. ], [ 1,2,3]); +/// assert_eq!(arr[1..3], [ 1,2 ]); +/// ``` #[derive(Copy, Clone, PartialEq, Eq, Hash)] #[stable(feature = "rust1", since = "1.0.0")] pub struct RangeTo<Idx> { @@ -2011,16 +2027,31 @@ impl<Idx: PartialOrd<Idx>> RangeInclusive<Idx> { /// /// # Examples /// +/// The `...{integer}` syntax is a `RangeToInclusive`: +/// /// ``` /// #![feature(inclusive_range,inclusive_range_syntax)] -/// fn main() { -/// assert_eq!((...5), std::ops::RangeToInclusive{ end: 5 }); +/// assert_eq!((...5), std::ops::RangeToInclusive{ end: 5 }); +/// ``` /// -/// let arr = [0, 1, 2, 3]; -/// assert_eq!(arr[ ...2], [0,1,2 ]); // RangeToInclusive -/// assert_eq!(arr[1...2], [ 1,2 ]); +/// It does not have an `IntoIterator` implementation, so you can't use it in a +/// `for` loop directly. This won't compile: +/// +/// ```ignore +/// for i in ...5 { +/// // ... /// } /// ``` +/// +/// When used as a slicing index, `RangeToInclusive` produces a slice of all +/// array elements up to and including the index indicated by `end`. +/// +/// ``` +/// #![feature(inclusive_range_syntax)] +/// let arr = [0, 1, 2, 3]; +/// assert_eq!(arr[ ...2], [0,1,2 ]); // RangeToInclusive +/// assert_eq!(arr[1...2], [ 1,2 ]); +/// ``` #[derive(Copy, Clone, PartialEq, Eq, Hash)] #[unstable(feature = "inclusive_range", reason = "recently added, follows RFC", issue = "28237")] pub struct RangeToInclusive<Idx> { |
