From d52744e9fc502fcc5ac6fa2003505d9ca0fcb6a4 Mon Sep 17 00:00:00 2001 From: Marijn Schouten Date: Thu, 2 Oct 2025 07:12:08 +0000 Subject: [PATCH] iter repeat: panic on last --- library/core/src/iter/sources/repeat.rs | 3 ++- library/coretests/tests/iter/sources.rs | 1 + 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/library/core/src/iter/sources/repeat.rs b/library/core/src/iter/sources/repeat.rs index 4bcd5b16aea6a..ac218e9b617a3 100644 --- a/library/core/src/iter/sources/repeat.rs +++ b/library/core/src/iter/sources/repeat.rs @@ -97,8 +97,9 @@ impl Iterator for Repeat { Some(self.element.clone()) } + #[track_caller] fn last(self) -> Option { - Some(self.element) + panic!("iterator is infinite"); } #[track_caller] diff --git a/library/coretests/tests/iter/sources.rs b/library/coretests/tests/iter/sources.rs index 5a391cb67751d..420f3088e6ee4 100644 --- a/library/coretests/tests/iter/sources.rs +++ b/library/coretests/tests/iter/sources.rs @@ -37,6 +37,7 @@ fn test_repeat_count() { } #[test] +#[should_panic = "iterator is infinite"] fn test_repeat_last() { assert_eq!(repeat(42).last(), Some(42)); }