diff options
| author | Mazdak Farrokhzad <twingoow@gmail.com> | 2019-07-10 16:08:23 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2019-07-10 16:08:23 +0200 |
| commit | ad21558b8539a8a9bcb21800ad1b7918304d7ef7 (patch) | |
| tree | 77441862060c085bff64fb1153dceea63d8fcfbc /src/libcore | |
| parent | 5760bc6e987c5b359a951793b6a9cf66a3b719ec (diff) | |
| parent | 76a8bc2473e60be35f96ecc268c6bb1d992ede78 (diff) | |
| download | rust-ad21558b8539a8a9bcb21800ad1b7918304d7ef7.tar.gz rust-ad21558b8539a8a9bcb21800ad1b7918304d7ef7.zip | |
Rollup merge of #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!
Diffstat (limited to 'src/libcore')
| -rw-r--r-- | src/libcore/iter/traits/iterator.rs | 4 |
1 files changed, 1 insertions, 3 deletions
diff --git a/src/libcore/iter/traits/iterator.rs b/src/libcore/iter/traits/iterator.rs index 6eddac672c1..7e941267ce8 100644 --- a/src/libcore/iter/traits/iterator.rs +++ b/src/libcore/iter/traits/iterator.rs @@ -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. |
