]> git.lizzy.rs Git - rust.git/commitdiff
Make Zip iterator short-circuit
authorKevin Ballard <kevin@sb.org>
Sat, 3 Aug 2013 22:41:29 +0000 (15:41 -0700)
committerKevin Ballard <kevin@sb.org>
Fri, 30 Aug 2013 05:49:26 +0000 (22:49 -0700)
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

index 841b6134410e614a4f9dbb08c5add52957026af3..3b4c31349c9c27881b83dff4e85dd861102ef175 100644 (file)
@@ -929,9 +929,12 @@ pub struct Zip<T, U> {
 impl<A, B, T: Iterator<A>, U: Iterator<B>> Iterator<(A, B)> for Zip<T, U> {
     #[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 @@ fn indexable(&self) -> uint {
 
     #[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))
+            }
         }
     }
 }