Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Consider tuple iteration #930

Open
brson opened this issue Mar 3, 2015 · 6 comments
Open

Consider tuple iteration #930

brson opened this issue Mar 3, 2015 · 6 comments
Labels
T-lang Relevant to the language team, which will review and decide on the RFC. T-libs-api Relevant to the library API team, which will review and decide on the RFC.

Comments

@brson
Copy link
Contributor

brson commented Mar 3, 2015

Original proposed in #870, some syntax sugar for iterating over multiple things.

@tomjakubowski
Copy link
Contributor

It doesn't really need to be syntax sugar. If it's done as an IntoIterator impl on tuples then it's not exactly syntax, is it?

I've written code like:

for (bx, (by, bz)) in xs.iter().zip(ys.iter().zip(zs.iter_mut())) { }

too many times. This would be much appreciated and make iterating over several iterables at once far easier to read, in my opinion.

@devyn
Copy link

devyn commented Aug 25, 2016

I would love to see this. Zip syntax right now is kind of ugly and unreadable, IMHO. I almost prefer to use indices instead of iterators half the time. I don't think there's any reason why we couldn't have both.

@Amanieu
Copy link
Member

Amanieu commented Aug 25, 2016

See #1650 which presents one way to do this.

@nrc nrc added T-lang Relevant to the language team, which will review and decide on the RFC. T-libs-api Relevant to the library API team, which will review and decide on the RFC. labels Aug 25, 2016
@devyn
Copy link

devyn commented Aug 26, 2016

I'm not really sure I see how that would make this possible. I'm just looking for an impl of IntoIterator for the tuples where every element implements IntoIterator that behaves somewhat like zip does.

That is,

for x in (&[1, 2, 3], &[4, 5, 6]) {
    println!("{:?}", x);
}

would be equivalent to

for x in (&[1,2,3]).into_iter().zip(&[4,5,6]) {
    println!("{:?}", x);
}

@Amanieu
Copy link
Member

Amanieu commented Aug 26, 2016

Ah sorry, I misunderstood what you were asking for. What I had in mind was somelike like a map operation that you could perform on each element of a tuple, even if they have different types.

@Kixunil
Copy link

Kixunil commented Jul 20, 2017

Should rust-lang/rust#43242 be part of this RFC or a separate one?

cuviper added a commit to cuviper/rust that referenced this issue Oct 21, 2020
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
T-lang Relevant to the language team, which will review and decide on the RFC. T-libs-api Relevant to the library API team, which will review and decide on the RFC.
Projects
None yet
Development

No branches or pull requests

6 participants