Skip to content

Commit

Permalink
Rollup merge of rust-lang#62481 - czipperz:iterator-last-nth-use-for_…
Browse files Browse the repository at this point in the history
…each, r=scottmcm

Use `fold` in `Iterator::last` default implementation

We already use it in all the other methods.  Consistency + potential perf is a pretty nice win!
  • Loading branch information
Centril committed Jul 10, 2019
2 parents 5760bc6 + 76a8bc2 commit ad21558
Showing 1 changed file with 1 addition and 3 deletions.
4 changes: 1 addition & 3 deletions src/libcore/iter/traits/iterator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit ad21558

Please sign in to comment.