Skip to content

Commit 88a99cd

Browse files
committed
repeat iter: tests + make map last work
1 parent 40ace17 commit 88a99cd

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

library/core/src/iter/adapters/map.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,13 @@ where
112112
self.iter.size_hint()
113113
}
114114

115+
fn last(mut self) -> Option<Self::Item>
116+
where
117+
Self: Sized,
118+
{
119+
self.iter.last().map(&mut self.f)
120+
}
121+
115122
fn try_fold<Acc, G, R>(&mut self, init: Acc, g: G) -> R
116123
where
117124
Self: Sized,

library/coretests/tests/iter/sources.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,22 @@ fn test_repeat_take_collect() {
3030
assert_eq!(v, vec![42, 42, 42]);
3131
}
3232

33+
#[test]
34+
#[should_panic = "iterator is infinite"]
35+
fn test_repeat_count() {
36+
repeat(42).count();
37+
}
38+
39+
#[test]
40+
fn test_repeat_last() {
41+
assert_eq!(repeat(42).last(), Some(42));
42+
}
43+
44+
#[test]
45+
fn test_repeat_map_double_last() {
46+
assert_eq!(repeat(42).map(|e| 2 * e).last(), Some(2 * 42));
47+
}
48+
3349
#[test]
3450
fn test_repeat_with() {
3551
#[derive(PartialEq, Debug)]

0 commit comments

Comments
 (0)