# What do you think about making a and b fields of std::iter::Zip public?

**URL:** <https://internals.rust-lang.org/t/what-do-you-think-about-making-a-and-b-fields-of-std-zip-public/14264>\
**Category:** libs\
**Created:** [March 14, 2021, 5:50pm UTC](https://internals.rust-lang.org/t/what-do-you-think-about-making-a-and-b-fields-of-std-zip-public/14264 "2021-03-14T17:50:52Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![ouaicava](https://avatars.discourse-cdn.com/v4/letter/o/76d3ee/32.png) [@ouaicava](https://internals.rust-lang.org/u/ouaicava)\
**Post date:** [March 14, 2021, 5:50pm UTC](https://internals.rust-lang.org/t/what-do-you-think-about-making-a-and-b-fields-of-std-zip-public/14264/1 "2021-03-14T17:50:52Z")

</div>

The iterator zip stops returning elements once one of the two iterators is empty.

There are no easy way of consuming the remaining elements of the longest iterator.

A solution to this would be making the `a` and `b` fields of [the Zip struct](https://doc.rust-lang.org/src/core/iter/adapters/zip.rs.html#13-19) public.

Another solution would be to add a `rest` method that keeps returning elements from the longest iterator. This could cause a problem with `next_back`.

What do you think about this ?

---

<div class="post-metadata">

**Author:** ![steffahn](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/steffahn/32/13288_2.png) [@steffahn](https://internals.rust-lang.org/u/steffahn)\
**Post date:** [March 14, 2021, 6:10pm UTC](https://internals.rust-lang.org/t/what-do-you-think-about-making-a-and-b-fields-of-std-zip-public/14264/2 "2021-03-14T18:10:45Z")

</div>

You need to address this problem raised in the URLO thread you opened yesterday.

> **[Consume the remaining elements of a Zip](https://users.rust-lang.org/t/consume-the-remaining-elements-of-a-zip/56884/3)**
>
> If you print the sum after running the code you posted, you will understand why zip doesn't let you get back the inner iterators. Doing so will lose the first extra message in the longer iterator, and to avoid it, you need some sort of peeking...

Just making the fields public doesn't solve the problem of skipping a value for the longer iterator, at least if iterator `a` is the longer one.

Of course it's feasible to extend `Zip` in a way that saves this otherwise forgotten value and offers some access to the remaining elements, e.g. through another iterator or some other kinds of methods like you proposed.

---

<div class="post-metadata">

**Author:** ![steffahn](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/steffahn/32/13288_2.png) [@steffahn](https://internals.rust-lang.org/u/steffahn)\
**Post date:** [March 14, 2021, 7:04pm UTC](https://internals.rust-lang.org/t/what-do-you-think-about-making-a-and-b-fields-of-std-zip-public/14264/3 "2021-03-14T19:04:58Z")

</div>

Another small problem is that not immediately dropping the first element of `a` on the last call to `.next()` would make a small but observable change to the behavior of `Zip`.

A bigger problem is the `DoubleEndedIterator` implementation. Currently if you zip two `DoubleEndedIterator + ExactSizeIterator` iterators then the resulting `Zip` also supports `DoubleEndedIterator` operation such as `next_back()`. For this, the implementation does—on the first call to `next_back`—first trim the longer iterator so that they both have the same length, with the goal that the iterators are still, logically, being “zipped from the front” (i.e. you _really_ get the same list of values, just backwards [as well as reversed side-effects]). The problem is: An iterator does (in general) not allow you to just “leave” the last `n` elements alone and save them for later. The only way in which the rest/remainder of `Zip` could be kept after a call to `next_back` is by storing them in a new `Vec` or something; while caching a single element is reasonable, doing a whole potentially large allocation _“just-in-case-it’s-needed”_ for an uncommon use-case seems very unreasonable.

Possible solutions include panicking or otherwise failing on calling a potential `rest`/`remainder` method in case `next_back` was called. Maybe the more reasonable approach is to just introduce an alternative to `zip` that offers such a `rest` method (and doesn’t implement `DoubleEndedIterator`) and maybe do this only in a third-party-crate.

---

<div class="post-metadata">

**Author:** ![scottmcm](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/scottmcm/32/2355_2.png) [@scottmcm](https://internals.rust-lang.org/u/scottmcm)\
**Post date:** [March 14, 2021, 7:27pm UTC](https://internals.rust-lang.org/t/what-do-you-think-about-making-a-and-b-fields-of-std-zip-public/14264/4 "2021-03-14T19:27:00Z")

</div>

> [@ouaicava](#):
>
> What do you think about this ?

My first instinct here is that I can't think of any other iterator adapter with a public field, so I'm skeptical. Maybe a method to expose something (like the slice iterators have methods to give out slices), but probably not a field.

But broader than that, it makes me wonder if Zip is even the right thing for this use case. Can you describe why you want this? Are the iterators ESI? Could it be better done with [`zip_longest`](https://docs.rs/itertools/0.10.0/itertools/trait.Itertools.html#method.zip_longest)? Etc.

---

<div class="post-metadata">

**Author:** ![H2CO3](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/h2co3/32/2849_2.png) [@H2CO3](https://internals.rust-lang.org/u/H2CO3)\
**Post date:** [March 14, 2021, 7:38pm UTC](https://internals.rust-lang.org/t/what-do-you-think-about-making-a-and-b-fields-of-std-zip-public/14264/5 "2021-03-14T19:38:40Z")

</div>

Do also note that it's not an accident the fields are private. In general, types that need to uphold their internal invariants can't just expose fields as public, because anyone could then mutate those fields in a way that would cause the state of the type to be inconsistent.

---

<div class="post-metadata">

**Author:** ![jhpratt](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/jhpratt/32/11640_2.png) [@jhpratt](https://internals.rust-lang.org/u/jhpratt)\
**Post date:** [March 14, 2021, 7:53pm UTC](https://internals.rust-lang.org/t/what-do-you-think-about-making-a-and-b-fields-of-std-zip-public/14264/6 "2021-03-14T19:53:19Z")

</div>

_cough_ use case for read only fields

---

<div class="post-metadata">

**Author:** ![H2CO3](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/h2co3/32/2849_2.png) [@H2CO3](https://internals.rust-lang.org/u/H2CO3)\
**Post date:** [March 14, 2021, 8:34pm UTC](https://internals.rust-lang.org/t/what-do-you-think-about-making-a-and-b-fields-of-std-zip-public/14264/7 "2021-03-14T20:34:30Z")

</div>

Meh. You could just write a getter for that. But more importantly, what do you do with a general read-only iterator? You can't even `.count()` it unless you own the whole `Zip`.

---

<div class="post-metadata">

**Author:** ![cuviper](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/cuviper/32/1897_2.png) [@cuviper](https://internals.rust-lang.org/u/cuviper)\
**Post date:** [March 15, 2021, 12:49am UTC](https://internals.rust-lang.org/t/what-do-you-think-about-making-a-and-b-fields-of-std-zip-public/14264/8 "2021-03-15T00:49:12Z")

</div>

> [@ouaicava](#):
>
> A solution to this would be making the `a` and `b` fields of [the Zip struct](https://doc.rust-lang.org/src/core/iter/adapters/zip.rs.html#13-19) public.

I fear what public fields would mean for all the `unsafe impl` and specialization used by `Zip`. I don't know any specific way that would be a problem at the moment, but the risk would be increased.

---

<div class="post-metadata">

**Author:** ![steffahn](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/steffahn/32/13288_2.png) [@steffahn](https://internals.rust-lang.org/u/steffahn)\
**Post date:** [March 15, 2021, 1:25am UTC](https://internals.rust-lang.org/t/what-do-you-think-about-making-a-and-b-fields-of-std-zip-public/14264/9 "2021-03-15T01:25:14Z")

</div>

> [@cuviper](#):
>
> I fear what public fields would mean for all the `unsafe impl` and specialization used by `Zip` . I don't know any specific way that would be a problem at the moment, but the risk would be increased.

Oh, I’m certain that it’s unsound to make the fields public with the current `Zip` implementation. For example, look at [`Vec::IntoIter`’s implementation of `__iterator_get_unchecked`](https://doc.rust-lang.org/nightly/src/alloc/vec/into_iter.rs.html#156-172) and [how `Zip` uses it](https://doc.rust-lang.org/nightly/src/core/iter/adapters/zip.rs.html#194-216) and think about the _lovely ways_ it could read beyond the end of the vector’s allocation if you threw a few direct calls to `.next()` (increasing `self.ptr`) on `Zip`’s fields into the mix if they were public.

---

<div class="post-metadata">

**Author:** ![2e71828](https://avatars.discourse-cdn.com/v4/letter/2/3e96dc/32.png) [@2e71828](https://internals.rust-lang.org/u/2e71828)\
**Post date:** [March 15, 2021, 9:10am UTC](https://internals.rust-lang.org/t/what-do-you-think-about-making-a-and-b-fields-of-std-zip-public/14264/10 "2021-03-15T09:10:29Z")

</div>

An alternative would be an `into_inner(self: Zip<A,B>)->(A,B)` method to get the unconsumed portion of the original iterators back. There's no danger of violating `Zip`'s invariants if the `Zip` object doesn't exist anymore.

---

<div class="post-metadata">

**Author:** ![matklad](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/matklad/32/12266_2.png) [@matklad](https://internals.rust-lang.org/u/matklad)\
**Post date:** [March 15, 2021, 9:14am UTC](https://internals.rust-lang.org/t/what-do-you-think-about-making-a-and-b-fields-of-std-zip-public/14264/11 "2021-03-15T09:14:14Z")

</div>

I believe this is handled by the [by\_ref](https://doc.rust-lang.org/stable/std/iter/trait.Iterator.html#method.by_ref) method? (modulo the issue that one element is skipped anyway).

---

<div class="post-metadata">

**Author:** ![2e71828](https://avatars.discourse-cdn.com/v4/letter/2/3e96dc/32.png) [@2e71828](https://internals.rust-lang.org/u/2e71828)\
**Post date:** [March 15, 2021, 9:24am UTC](https://internals.rust-lang.org/t/what-do-you-think-about-making-a-and-b-fields-of-std-zip-public/14264/12 "2021-03-15T09:24:09Z")

</div>

`by_ref` serves a similar purpose, but must be called _before_ creating the `Zip`. It can't be used to destructure a `Zip` that was received as an argument or returned from a function.

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/system/32/14092_2.png) [@system](https://internals.rust-lang.org/u/system)\
**Post date:** [June 13, 2021, 9:25am UTC](https://internals.rust-lang.org/t/what-do-you-think-about-making-a-and-b-fields-of-std-zip-public/14264/13 "2021-06-13T09:25:02Z")

</div>

Closed on June 13, 2021, 9:25am UTC

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.
