]> git.lizzy.rs Git - rust.git/commitdiff
Use fold in Iterator::last
authorChris Gregory <czipperz@gmail.com>
Sun, 7 Jul 2019 23:54:48 +0000 (16:54 -0700)
committerChris Gregory <czipperz@gmail.com>
Wed, 10 Jul 2019 04:26:49 +0000 (21:26 -0700)
Replace last impl with fold

src/libcore/iter/traits/iterator.rs

index 30923c7414504c1a6863d9b50cec569c668ac430..d934b20a33fac14833dff3ff3b6e2cdf85469b4b 100644 (file)
@@ -263,9 +263,7 @@ fn count(self) -> usize where Self: Sized {
     #[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.