Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions library/core/src/iter/adapters/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,13 @@ where
self.iter.size_hint()
}

fn last(mut self) -> Option<Self::Item>
where
Self: Sized,
{
self.iter.last().map(&mut self.f)
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we should do this; it skips calling f on all of the elements up to the last one, which doesn't seem right to me, especially since we do iterate over those elements (i.e., we aren't specialized in some way for DoubleEndedIterator).

If we do proceed with this it'll need libs-api FCP, so nominating this PR.


fn try_fold<Acc, G, R>(&mut self, init: Acc, g: G) -> R
where
Self: Sized,
Expand Down
16 changes: 16 additions & 0 deletions library/coretests/tests/iter/sources.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,22 @@ fn test_repeat_take_collect() {
assert_eq!(v, vec![42, 42, 42]);
}

#[test]
#[should_panic = "iterator is infinite"]
fn test_repeat_count() {
repeat(42).count();
}

#[test]
fn test_repeat_last() {
assert_eq!(repeat(42).last(), Some(42));
}

#[test]
fn test_repeat_map_double_last() {
assert_eq!(repeat(42).map(|e| 2 * e).last(), Some(2 * 42));
}

#[test]
fn test_repeat_with() {
#[derive(PartialEq, Debug)]
Expand Down
Loading