Skip to content

Commit a3d18bc

Browse files
committed
Make Zip iterator short-circuit
Python's zip() short-circuits by not even querying its right-hand iterator if the left-hand one is done. Match that behavior here by not calling .next() on the right iterator if the left one returns None.
1 parent fb0b388 commit a3d18bc

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

src/libstd/iterator.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -929,9 +929,12 @@ pub struct Zip<T, U> {
929929
impl<A, B, T: Iterator<A>, U: Iterator<B>> Iterator<(A, B)> for Zip<T, U> {
930930
#[inline]
931931
fn next(&mut self) -> Option<(A, B)> {
932-
match (self.a.next(), self.b.next()) {
933-
(Some(x), Some(y)) => Some((x, y)),
934-
_ => None
932+
match self.a.next() {
933+
None => None,
934+
Some(x) => match self.b.next() {
935+
None => None,
936+
Some(y) => Some((x, y))
937+
}
935938
}
936939
}
937940

@@ -962,9 +965,12 @@ RandomAccessIterator<(A, B)> for Zip<T, U> {
962965

963966
#[inline]
964967
fn idx(&self, index: uint) -> Option<(A, B)> {
965-
match (self.a.idx(index), self.b.idx(index)) {
966-
(Some(x), Some(y)) => Some((x, y)),
967-
_ => None
968+
match self.a.idx(index) {
969+
None => None,
970+
Some(x) => match self.b.idx(index) {
971+
None => None,
972+
Some(y) => Some((x, y))
973+
}
968974
}
969975
}
970976
}

0 commit comments

Comments
 (0)