diff --git a/src/zip_eq_impl.rs b/src/zip_eq_impl.rs index 8139c2cce..2736d3854 100644 --- a/src/zip_eq_impl.rs +++ b/src/zip_eq_impl.rs @@ -52,7 +52,6 @@ where } } - #[inline] fn fold(self, init: B, mut f: F) -> B where Self: Sized, @@ -60,10 +59,16 @@ where { let Self { a, mut b } = self; - a.fold(init, |acc, x| match b.next() { + let acc = a.fold(init, |acc, x| match b.next() { Some(y) => f(acc, (x, y)), None => panic!("itertools: .zip_eq() reached end of one iterator before the other"), - }) + }); + + if b.next().is_some() { + panic!("itertools: .zip_eq() reached end of one iterator before the other") + } + + acc } fn size_hint(&self) -> (usize, Option) { diff --git a/tests/zip.rs b/tests/zip.rs index 9efc03fc4..be886da7a 100644 --- a/tests/zip.rs +++ b/tests/zip.rs @@ -82,3 +82,12 @@ fn zip_eq_panic3() { zip_eq(&a, &b).fold(0, |acc, (x, y)| acc + x * y); } + +#[should_panic] +#[test] +fn zip_eq_panic4() { + let b = [1, 2]; + let a = [1, 2, 3]; + + zip_eq(&a, &b).fold(0, |acc, (x, y)| acc + x * y); +}