Skip to content

Implement Iter::drain(&mut self) with feature iterator_helper #45168

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

Closed
wants to merge 1 commit 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
18 changes: 18 additions & 0 deletions src/libcore/iter/iterator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1514,6 +1514,24 @@ pub trait Iterator {
false
}

/// Completly consumes an iterator. This is a conveniant method for
/// functional programming with iterators.
///
/// ```
/// let xs = [0, 1, 2, 3];
/// let mut sum = 0;
///
/// xs.iter().map(|x| sum += x).drain();
Copy link
Member

Choose a reason for hiding this comment

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

This is a more complex way to write xs.iter().sum() or even xs.iter().for_each(|x| sum += x).

/// assert_eq!(6, sum);
/// ```
#[inline]
#[stable(feature = "iterator_helper", since = "1.22.0")]
Copy link
Member

Choose a reason for hiding this comment

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

It's a minor thing, but to keep it as predictable as possible, iter_methodname or iterator_methodname would be a good feature name. Depends on what the method name will be, of course, just change it with the method name.

fn drain(&mut self) where
Self: Sized
{
self.fold((), |_,_| {});
}

/// Searches for an element of an iterator that satisfies a predicate.
///
/// `find()` takes a closure that returns `true` or `false`. It applies
Expand Down