Skip to content
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

Add Iterator::exhaust #49990

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 27 additions & 1 deletion src/libcore/iter/iterator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2223,7 +2223,7 @@ pub trait Iterator {
Sum::sum(self)
}

/// Iterates over the entire iterator, multiplying all the elements
/// Iterates over the entire iterator, multiplying all the elements.
///
/// An empty iterator returns the one value of the type.
///
Expand Down Expand Up @@ -2251,6 +2251,32 @@ pub trait Iterator {
Product::product(self)
}

/// Eagerly consume the iterator, evaluating all the elements.
///
/// This is primarily useful for evaluating the effects of an iterator that
/// has previously been created. To evaluate the effects of an iterator
/// immediately, it is likely better to use [`for_each`].
///
/// [`for_each`]: #method.for_each
///
/// # Examples
///
/// ```
/// // Prepare an iterator with side effects...
/// let count_aloud = 1..=10.map(|x| println!("{}", x));
///
/// // ...
///
/// // Trigger the effects!
/// count_aloud.exhaust();
///
/// ```
#[inline]
#[unstable(feature = "iter_exhaust", issue = "44546")]
fn exhaust(self) where Self: Sized {
for _ in self {}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should use for_each to get internal iteration, rather than (implicit) next() calls.

Copy link
Member Author

@varkor varkor Apr 16, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It doesn't matter so much now the PR's closed, but out of curiosity, what difference in behaviour would using for_each have?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The difference is that some iterators can apply a fold much more efficiently than they can next one item at a time. For instance, Chain::fold can fold its first part as a whole, then its second part as a whole, but Chain::next has to check whether it's on the first or second part every time. This is demonstrated in the benchmarks added to the for_each PR, #42782, in commit 4a8ddac.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, of course, that makes a lot of sense. Thanks for explaining!

}

/// Lexicographically compares the elements of this `Iterator` with those
/// of another.
#[stable(feature = "iter_order", since = "1.5.0")]
Expand Down