Use fold in Iterator::last

Replace last impl with fold
This commit is contained in:
Chris Gregory 2019-07-07 16:54:48 -07:00
parent 481068a707
commit 76a8bc2473

View file

@ -263,9 +263,7 @@ pub trait Iterator {
#[inline]
#[stable(feature = "rust1", since = "1.0.0")]
fn last(self) -> Option<Self::Item> where Self: Sized {
let mut last = None;
for x in self { last = Some(x); }
last
self.fold(None, |_, x| Some(x))
}
/// Returns the `n`th element of the iterator.