Skip to content
Open
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)
}

fn try_fold<Acc, G, R>(&mut self, init: Acc, g: G) -> R
where
Self: Sized,
Expand Down
6 changes: 5 additions & 1 deletion library/core/src/iter/sources/repeat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,11 @@ impl<A: Clone> Iterator for Repeat<A> {
}

#[track_caller]
fn count(self) -> usize {
fn fold<B, F>(self, _init: B, _f: F) -> B
where
Self: Sized,
F: FnMut(B, Self::Item) -> B,
{
panic!("iterator is infinite");
}
}
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