diff options
| author | Kevin Ballard <kevin@sb.org> | 2013-08-03 15:41:29 -0700 |
|---|---|---|
| committer | Kevin Ballard <kevin@sb.org> | 2013-08-29 22:49:26 -0700 |
| commit | a3d18bc95b1639cf3cb967165f7104e2c79893f5 (patch) | |
| tree | baa14af3e79204f372cc692e79a0ed8c3e6ee5eb | |
| parent | fb0b388804ec6b4535e73a890feda7372182486f (diff) | |
| download | rust-a3d18bc95b1639cf3cb967165f7104e2c79893f5.tar.gz rust-a3d18bc95b1639cf3cb967165f7104e2c79893f5.zip | |
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.
| -rw-r--r-- | src/libstd/iterator.rs | 18 |
1 files changed, 12 insertions, 6 deletions
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<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 @@ RandomAccessIterator<(A, B)> for Zip<T, U> { #[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)) + } } } } |
