Skip to content

Commit ad21558

Browse files
authored
Rollup merge of rust-lang#62481 - czipperz:iterator-last-nth-use-for_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!
2 parents 5760bc6 + 76a8bc2 commit ad21558

File tree

1 file changed

+1
-3
lines changed

1 file changed

+1
-3
lines changed

src/libcore/iter/traits/iterator.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -263,9 +263,7 @@ pub trait Iterator {
263263
#[inline]
264264
#[stable(feature = "rust1", since = "1.0.0")]
265265
fn last(self) -> Option<Self::Item> where Self: Sized {
266-
let mut last = None;
267-
for x in self { last = Some(x); }
268-
last
266+
self.fold(None, |_, x| Some(x))
269267
}
270268

271269
/// Returns the `n`th element of the iterator.

0 commit comments

Comments
 (0)