|
2 | 2 |
|
3 | 3 | use crate::cmp::Ordering::*;
|
4 | 4 | use crate::cmp::*;
|
| 5 | +use crate::iter::{IntoIterator, Zip}; |
5 | 6 |
|
6 | 7 | // macro for implementing n-ary tuple functions and operations
|
7 | 8 | macro_rules! tuple_impls {
|
@@ -211,3 +212,39 @@ tuple_impls! {
|
211 | 212 | (11) -> L
|
212 | 213 | }
|
213 | 214 | }
|
| 215 | + |
| 216 | +/// Converts a pair of `IntoIterator` values into a single iterator of pairs |
| 217 | +/// |
| 218 | +/// This works like an implicit [`zip`] in any context that accepts `IntoIterator` |
| 219 | +/// values, like [`chain`], [`flat_map`], and especially `for` loops. |
| 220 | +/// |
| 221 | +/// ``` |
| 222 | +/// let xs = [1, 2, 3]; |
| 223 | +/// let ys = [4, 5, 6]; |
| 224 | +/// for (x, y) in (&xs, &ys) { |
| 225 | +/// println!("x:{}, y:{}", x, y); |
| 226 | +/// } |
| 227 | +/// |
| 228 | +/// // Nested zips are also possible: |
| 229 | +/// let zs = [7, 8, 9]; |
| 230 | +/// for ((x, y), z) in ((&xs, &ys), &zs) { |
| 231 | +/// println!("x:{}, y:{}, z:{}", x, y, z); |
| 232 | +/// } |
| 233 | +/// ``` |
| 234 | +/// |
| 235 | +/// [`chain`]: Iterator::chain |
| 236 | +/// [`flat_map`]: Iterator::flat_map |
| 237 | +/// [`zip`]: Iterator::zip |
| 238 | +#[stable(feature = "pair_into_iter", since = "1.49.0")] |
| 239 | +impl<A, B> IntoIterator for (A, B) |
| 240 | +where |
| 241 | + A: IntoIterator, |
| 242 | + B: IntoIterator, |
| 243 | +{ |
| 244 | + type IntoIter = Zip<A::IntoIter, B::IntoIter>; |
| 245 | + type Item = (A::Item, B::Item); |
| 246 | + |
| 247 | + fn into_iter(self) -> Self::IntoIter { |
| 248 | + self.0.into_iter().zip(self.1) |
| 249 | + } |
| 250 | +} |
0 commit comments