From 88a99cd7ff6a63c4e8286d1a9e02c82493a9cd3d Mon Sep 17 00:00:00 2001 From: Marijn Schouten Date: Wed, 17 Sep 2025 13:09:23 +0000 Subject: [PATCH] repeat iter: tests + make map last work --- library/core/src/iter/adapters/map.rs | 7 +++++++ library/coretests/tests/iter/sources.rs | 16 ++++++++++++++++ 2 files changed, 23 insertions(+) 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)]