Skip to content

Commit 18e352d

Browse files
committed
Ensure panic on length mismatch
1 parent a373e69 commit 18e352d

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

src/zip_eq_impl.rs

+8-3
Original file line numberDiff line numberDiff line change
@@ -52,18 +52,23 @@ where
5252
}
5353
}
5454

55-
#[inline]
5655
fn fold<B, F>(self, init: B, mut f: F) -> B
5756
where
5857
Self: Sized,
5958
F: FnMut(B, Self::Item) -> B,
6059
{
6160
let Self { a, mut b } = self;
6261

63-
a.fold(init, |acc, x| match b.next() {
62+
let acc = a.fold(init, |acc, x| match b.next() {
6463
Some(y) => f(acc, (x, y)),
6564
None => panic!("itertools: .zip_eq() reached end of one iterator before the other"),
66-
})
65+
});
66+
67+
if b.next().is_some() {
68+
panic!("itertools: .zip_eq() reached end of one iterator before the other")
69+
}
70+
71+
acc
6772
}
6873

6974
fn size_hint(&self) -> (usize, Option<usize>) {

tests/zip.rs

+9
Original file line numberDiff line numberDiff line change
@@ -82,3 +82,12 @@ fn zip_eq_panic3() {
8282

8383
zip_eq(&a, &b).fold(0, |acc, (x, y)| acc + x * y);
8484
}
85+
86+
#[should_panic]
87+
#[test]
88+
fn zip_eq_panic4() {
89+
let b = [1, 2];
90+
let a = [1, 2, 3];
91+
92+
zip_eq(&a, &b).fold(0, |acc, (x, y)| acc + x * y);
93+
}

0 commit comments

Comments
 (0)