From 398ea759f15b453e7a40c81da6f3a38bf97d5977 Mon Sep 17 00:00:00 2001 From: Stefan Schindler Date: Tue, 10 Oct 2017 11:09:18 +0200 Subject: [PATCH] Implement Iter::drain(&mut self) with feature iterator_helper --- src/libcore/iter/iterator.rs | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/libcore/iter/iterator.rs b/src/libcore/iter/iterator.rs index e9e31065cf876..d932423957cc6 100644 --- a/src/libcore/iter/iterator.rs +++ b/src/libcore/iter/iterator.rs @@ -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(); + /// assert_eq!(6, sum); + /// ``` + #[inline] + #[stable(feature = "iterator_helper", since = "1.22.0")] + 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