diff options
| author | Mark Rousskov <mark.simulacrum@gmail.com> | 2018-07-11 12:38:34 -0600 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2018-07-11 12:38:34 -0600 |
| commit | 74cc821fd583844fb3e4a0ebbe78a1962e87c4bd (patch) | |
| tree | 892949bcdc1a9c285168d071532361a637d34347 | |
| parent | 322632ac108aa08c41a23e804a856f383d1b705f (diff) | |
| parent | 39fcfa8ccb4605e307eed4c7515b58c5edc97c74 (diff) | |
| download | rust-74cc821fd583844fb3e4a0ebbe78a1962e87c4bd.tar.gz rust-74cc821fd583844fb3e4a0ebbe78a1962e87c4bd.zip | |
Rollup merge of #52193 - Emerentius:step_by_note, r=alexcrichton
step_by: leave time of item skip unspecified This gives us some leeway when optimizing. `StepBy<RangeFrom<_>>` is one case where this is needed.
| -rw-r--r-- | src/libcore/iter/iterator.rs | 23 |
1 files changed, 22 insertions, 1 deletions
diff --git a/src/libcore/iter/iterator.rs b/src/libcore/iter/iterator.rs index 8836de3edc8..c0681619bf8 100644 --- a/src/libcore/iter/iterator.rs +++ b/src/libcore/iter/iterator.rs @@ -271,9 +271,30 @@ pub trait Iterator { /// 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 iterator, + /// Note 1: The first element of the iterator will always be returned, /// regardless of the step given. /// + /// Note 2: The time at which ignored elements are pulled is not fixed. + /// `StepBy` behaves like the sequence `next(), nth(step-1), nth(step-1), …`, + /// but is also free to behave like the sequence + /// `advance_n_and_return_first(step), advance_n_and_return_first(step), …` + /// Which way is used may change for some iterators for performance reasons. + /// The second way will advance the iterator earlier and may consume more items. + /// + /// `advance_n_and_return_first` is the equivalent of: + /// ``` + /// fn advance_n_and_return_first<I>(iter: &mut I, total_step: usize) -> Option<I::Item> + /// where + /// I: Iterator, + /// { + /// let next = iter.next(); + /// if total_step > 1 { + /// iter.nth(total_step-2); + /// } + /// next + /// } + /// ``` + /// /// # Panics /// /// The method will panic if the given step is `0`. |
