From f66cbbda70e9b007bbdd181a1167079126f50f58 Mon Sep 17 00:00:00 2001 From: Steven Fackler Date: Sat, 9 Mar 2019 22:02:54 -0800 Subject: [PATCH] Implement last in terms of fold --- src/lib.rs | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 4d8ed38..708ad77 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -147,15 +147,11 @@ pub trait FallibleIterator { /// Returns the last element of the iterator. #[inline] - fn last(mut self) -> Result, Self::Error> + fn last(self) -> Result, Self::Error> where Self: Sized, { - let mut last = None; - while let Some(e) = self.next()? { - last = Some(e); - } - Ok(last) + self.fold(None, |_, v| Ok(Some(v))) } /// Returns the `n`th element of the iterator.