diff --git a/library/core/src/iter/adapters/map.rs b/library/core/src/iter/adapters/map.rs index 007c2d5acc2d0..0f0498fcc7f0a 100644 --- a/library/core/src/iter/adapters/map.rs +++ b/library/core/src/iter/adapters/map.rs @@ -112,6 +112,13 @@ where self.iter.size_hint() } + fn last(mut self) -> Option + where + Self: Sized, + { + self.iter.last().map(&mut self.f) + } + fn try_fold(&mut self, init: Acc, g: G) -> R where Self: Sized, diff --git a/library/coretests/tests/iter/sources.rs b/library/coretests/tests/iter/sources.rs index 506febaa056a8..1397877a3106d 100644 --- a/library/coretests/tests/iter/sources.rs +++ b/library/coretests/tests/iter/sources.rs @@ -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)]