diff --git a/library/core/src/iter/sources/repeat.rs b/library/core/src/iter/sources/repeat.rs
index 4bcd5b16aea6..ac218e9b617a 100644
--- a/library/core/src/iter/sources/repeat.rs
+++ b/library/core/src/iter/sources/repeat.rs
@@ -97,8 +97,9 @@ fn nth(&mut self, n: usize) -> Option {
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 5a391cb67751..420f3088e6ee 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));
}