From a3d18bc95b1639cf3cb967165f7104e2c79893f5 Mon Sep 17 00:00:00 2001 From: Kevin Ballard Date: Sat, 3 Aug 2013 15:41:29 -0700 Subject: 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. --- src/libstd/iterator.rs | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) (limited to 'src/libstd/iterator.rs') diff --git a/src/libstd/iterator.rs b/src/libstd/iterator.rs index 841b6134410..3b4c31349c9 100644 --- a/src/libstd/iterator.rs +++ b/src/libstd/iterator.rs @@ -929,9 +929,12 @@ pub struct Zip { impl, U: Iterator> Iterator<(A, B)> for Zip { #[inline] fn next(&mut self) -> Option<(A, B)> { - match (self.a.next(), self.b.next()) { - (Some(x), Some(y)) => Some((x, y)), - _ => None + match self.a.next() { + None => None, + Some(x) => match self.b.next() { + None => None, + Some(y) => Some((x, y)) + } } } @@ -962,9 +965,12 @@ RandomAccessIterator<(A, B)> for Zip { #[inline] fn idx(&self, index: uint) -> Option<(A, B)> { - match (self.a.idx(index), self.b.idx(index)) { - (Some(x), Some(y)) => Some((x, y)), - _ => None + match self.a.idx(index) { + None => None, + Some(x) => match self.b.idx(index) { + None => None, + Some(y) => Some((x, y)) + } } } } -- cgit 1.4.1-3-g733a5