Skip to content

Commit 39fcfa8

Browse files
committed
step_by: leave time of item skip unspecified
this gives us some leeway when optimizing
1 parent bdd185c commit 39fcfa8

File tree

1 file changed

+22
-1
lines changed

1 file changed

+22
-1
lines changed

src/libcore/iter/iterator.rs

+22-1
Original file line numberDiff line numberDiff line change
@@ -271,9 +271,30 @@ pub trait Iterator {
271271
/// Creates an iterator starting at the same point, but stepping by
272272
/// the given amount at each iteration.
273273
///
274-
/// Note that it will always return the first element of the iterator,
274+
/// Note 1: The first element of the iterator will always be returned,
275275
/// regardless of the step given.
276276
///
277+
/// Note 2: The time at which ignored elements are pulled is not fixed.
278+
/// `StepBy` behaves like the sequence `next(), nth(step-1), nth(step-1), …`,
279+
/// but is also free to behave like the sequence
280+
/// `advance_n_and_return_first(step), advance_n_and_return_first(step), …`
281+
/// Which way is used may change for some iterators for performance reasons.
282+
/// The second way will advance the iterator earlier and may consume more items.
283+
///
284+
/// `advance_n_and_return_first` is the equivalent of:
285+
/// ```
286+
/// fn advance_n_and_return_first<I>(iter: &mut I, total_step: usize) -> Option<I::Item>
287+
/// where
288+
/// I: Iterator,
289+
/// {
290+
/// let next = iter.next();
291+
/// if total_step > 1 {
292+
/// iter.nth(total_step-2);
293+
/// }
294+
/// next
295+
/// }
296+
/// ```
297+
///
277298
/// # Panics
278299
///
279300
/// The method will panic if the given step is `0`.

0 commit comments

Comments
 (0)