about summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--library/core/src/iter/adapters/intersperse.rs2
-rw-r--r--library/core/tests/iter.rs12
2 files changed, 14 insertions, 0 deletions
diff --git a/library/core/src/iter/adapters/intersperse.rs b/library/core/src/iter/adapters/intersperse.rs
index 2143509d71d..b1170f32ba1 100644
--- a/library/core/src/iter/adapters/intersperse.rs
+++ b/library/core/src/iter/adapters/intersperse.rs
@@ -176,6 +176,8 @@ where
     if !needs_sep {
         if let Some(x) = iter.next() {
             accum = f(accum, x);
+        } else {
+            return accum;
         }
     }
 
diff --git a/library/core/tests/iter.rs b/library/core/tests/iter.rs
index fa259e68a43..78b4d1acaf5 100644
--- a/library/core/tests/iter.rs
+++ b/library/core/tests/iter.rs
@@ -3577,6 +3577,18 @@ fn test_intersperse_fold() {
         acc
     });
     assert_eq!(v.as_slice(), [9, 2, 9, 3]);
+
+    struct NoneAtStart(i32); // Produces: None, Some(2), Some(3), None, ...
+    impl Iterator for NoneAtStart {
+        type Item = i32;
+        fn next(&mut self) -> Option<i32> {
+            self.0 += 1;
+            Some(self.0).filter(|i| i % 3 != 1)
+        }
+    }
+
+    let v = NoneAtStart(0).intersperse(1000).fold(0, |a, b| a + b);
+    assert_eq!(v, 0);
 }
 
 #[test]