|
11 | 11 | use cmp::Ordering;
|
12 | 12 |
|
13 | 13 | use super::{Chain, Cycle, Cloned, Enumerate, Filter, FilterMap, FlatMap, Fuse};
|
14 |
| -use super::{Inspect, Map, Peekable, Scan, Skip, SkipWhile, Take, TakeWhile, Rev}; |
| 14 | +use super::{Inspect, Map, Peekable, Scan, Skip, SkipWhile, StepBy, Take, TakeWhile, Rev}; |
15 | 15 | use super::{Zip, Sum, Product};
|
16 | 16 | use super::{ChainState, FromIterator, ZipImpl};
|
17 | 17 |
|
@@ -258,6 +258,39 @@ pub trait Iterator {
|
258 | 258 | None
|
259 | 259 | }
|
260 | 260 |
|
| 261 | + /// Creates an iterator starting at the same point, but stepping by |
| 262 | + /// the given amount at each iteration. |
| 263 | + /// |
| 264 | + /// Note that it will always return the first element of the range, |
| 265 | + /// regardless of the step given. |
| 266 | + /// |
| 267 | + /// # Panics |
| 268 | + /// |
| 269 | + /// The method will panic if the given step is `0`. |
| 270 | + /// |
| 271 | + /// # Examples |
| 272 | + /// |
| 273 | + /// Basic usage: |
| 274 | + /// |
| 275 | + /// ``` |
| 276 | + /// #![feature(iterator_step_by)] |
| 277 | + /// let a = [0, 1, 2, 3, 4, 5]; |
| 278 | + /// let mut iter = a.into_iter().step_by(2); |
| 279 | + /// |
| 280 | + /// assert_eq!(iter.next(), Some(&0)); |
| 281 | + /// assert_eq!(iter.next(), Some(&2)); |
| 282 | + /// assert_eq!(iter.next(), Some(&4)); |
| 283 | + /// assert_eq!(iter.next(), None); |
| 284 | + /// ``` |
| 285 | + #[inline] |
| 286 | + #[unstable(feature = "iterator_step_by", |
| 287 | + reason = "unstable replacement of Range::step_by", |
| 288 | + issue = "27741")] |
| 289 | + fn step_by(self, step: usize) -> StepBy<Self> where Self: Sized { |
| 290 | + assert!(step != 0); |
| 291 | + StepBy{iter: self, step: step - 1, first_take: true} |
| 292 | + } |
| 293 | + |
261 | 294 | /// Takes two iterators and creates a new iterator over both in sequence.
|
262 | 295 | ///
|
263 | 296 | /// `chain()` will return a new iterator which will first iterate over
|
|
0 commit comments