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

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