Skip to content

Commit

Permalink
Implement last in terms of fold
Browse files Browse the repository at this point in the history
  • Loading branch information
sfackler committed Mar 10, 2019
1 parent 31fb3d4 commit f66cbbd
Showing 1 changed file with 2 additions and 6 deletions.
8 changes: 2 additions & 6 deletions src/lib.rs
Expand Up @@ -147,15 +147,11 @@ pub trait FallibleIterator {

/// Returns the last element of the iterator.
#[inline]
fn last(mut self) -> Result<Option<Self::Item>, Self::Error>
fn last(self) -> Result<Option<Self::Item>, 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.
Expand Down

0 comments on commit f66cbbd

Please sign in to comment.