-
Couldn't load subscription status.
- Fork 13.9k
Stabilize step_by by adding it to Iterator (issue #27741) #41439
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: This comment makes me think
debug_assert!, but it's not. Does it need more than "This method will panic if the givenstepis0."?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just looked at how the other methods in
Iteratorthat panic were phrased and tried doing something similar for consistency.I just checked now, and I didn't know of
debug_assert!, which is what the other methods are using. I've been mislead! :P I'll fix it soon. I also have to figure out what to write on the Unstable Book to make that tidy check pass 🙄