Skip to content

Commit f66cbbd

Browse files
committed
Implement last in terms of fold
1 parent 31fb3d4 commit f66cbbd

File tree

1 file changed

+2
-6
lines changed

1 file changed

+2
-6
lines changed

src/lib.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -147,15 +147,11 @@ pub trait FallibleIterator {
147147

148148
/// Returns the last element of the iterator.
149149
#[inline]
150-
fn last(mut self) -> Result<Option<Self::Item>, Self::Error>
150+
fn last(self) -> Result<Option<Self::Item>, Self::Error>
151151
where
152152
Self: Sized,
153153
{
154-
let mut last = None;
155-
while let Some(e) = self.next()? {
156-
last = Some(e);
157-
}
158-
Ok(last)
154+
self.fold(None, |_, v| Ok(Some(v)))
159155
}
160156

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

0 commit comments

Comments
 (0)