Skip to content

Commit 4decb2c

Browse files
committed
impl<A, B> IntoIterator for (A, B) as Zip
This makes it a little easier to `zip` iterators: ```rust for (x, y) in (xs, ys) {} // vs. for (x, y) in xs.into_iter().zip(ys) {} ``` You can iterate `(&mut xs, &ys)` for the conventional `iter_mut()` and `iter()`, respectively. This can also support arbitrary nesting, where it's easier to see the item layout than with arbitrary `zip` chains: ```rust for ((x, y), z) in ((xs, ys), zs) {} for (x, (y, z)) in (xs, (ys, zs)) {} // vs. for ((x, y), z) in xs.into_iter().zip(ys).zip(xz) {} for (x, (y, z)) in xs.into_iter().zip((ys.into_iter().zip(xz)) {} ``` Only tuple pairs are implemented for now. It's possible to implement longer tuples, but that would require a new iterator type to produce those tuple items. See itertools' [`Zip`] and rayon's [`MultiZip`] for prior art there. [`Zip`]: https://docs.rs/itertools/0.9.0/itertools/structs/struct.Zip.html [`MultiZip`]: https://docs.rs/rayon/1.4.1/rayon/iter/struct.MultiZip.html See also rust-lang/rfcs#870 and rust-lang/rfcs#930.
1 parent 1d27267 commit 4decb2c

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

library/core/src/tuple.rs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
use crate::cmp::Ordering::*;
44
use crate::cmp::*;
5+
use crate::iter::{IntoIterator, Zip};
56

67
// macro for implementing n-ary tuple functions and operations
78
macro_rules! tuple_impls {
@@ -211,3 +212,39 @@ tuple_impls! {
211212
(11) -> L
212213
}
213214
}
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

Comments
 (0)