about summary refs log tree commit diff
path: root/src/libcore/iter/iterator.rs
diff options
context:
space:
mode:
authorIvan Dardi <ivy.dardi@gmail.com>2017-05-12 15:11:15 -0300
committerIvan Dardi <ivy.dardi@gmail.com>2017-05-12 15:11:15 -0300
commitfa013725556fcf017aabef2e9c19837f45257b28 (patch)
tree9fc46cc6fb61653f82ff626ca979ba88d0a953dd /src/libcore/iter/iterator.rs
parente19ccb71c8427135a69d874623af68422aeeb9e9 (diff)
Fix commit derp and update implementations and documentation
Diffstat (limited to 'src/libcore/iter/iterator.rs')
-rw-r--r--src/libcore/iter/iterator.rs36
1 files changed, 35 insertions, 1 deletions
diff --git a/src/libcore/iter/iterator.rs b/src/libcore/iter/iterator.rs
index b3f4d75c4da..1277acfb8bb 100644
--- a/src/libcore/iter/iterator.rs
+++ b/src/libcore/iter/iterator.rs
@@ -11,7 +11,7 @@
 use cmp::Ordering;
 
 use super::{Chain, Cycle, Cloned, Enumerate, Filter, FilterMap, FlatMap, Fuse};
-use super::{Inspect, Map, Peekable, Scan, Skip, SkipWhile, Take, TakeWhile, Rev};
+use super::{Inspect, Map, Peekable, Scan, Skip, SkipWhile, StepBy, Take, TakeWhile, Rev};
 use super::{Zip, Sum, Product};
 use super::{ChainState, FromIterator, ZipImpl};
 
@@ -258,6 +258,40 @@ pub trait Iterator {
         None
     }
 
+    /// Creates an iterator starting at the same point, but stepping by
+    /// the given amount at each iteration.
+    ///
+    /// Note that it will always return the first element of the range,
+    /// regardless of the step given.
+    ///
+    /// # Panics
+    ///
+    /// If the given step is `0`, the method will panic if debug assertions are
+    /// enabled.
+    ///
+    /// # Examples
+    ///
+    /// Basic usage:
+    ///
+    /// ```
+    /// #![feature(iterator_step_by)]
+    /// let a = [0, 1, 2, 3, 4, 5];
+    /// let mut iter = a.into_iter().step_by(2);
+    ///
+    /// assert_eq!(iter.next(), Some(&0));
+    /// assert_eq!(iter.next(), Some(&2));
+    /// assert_eq!(iter.next(), Some(&4));
+    /// assert_eq!(iter.next(), None);
+    /// ```
+    #[inline]
+    #[unstable(feature = "iterator_step_by",
+               reason = "unstable replacement of Range::step_by",
+               issue = "27741")]
+    fn step_by(self, step: usize) -> StepBy<Self> where Self: Sized {
+        assert!(step != 0);
+        StepBy{iter: self, step: step - 1, first_take: true}
+    }
+
     /// Takes two iterators and creates a new iterator over both in sequence.
     ///
     /// `chain()` will return a new iterator which will first iterate over