# Implement ExactSizeIterator for \`Zip\<A, Repeat\<B\>\> where A: ExactSizeIterator, B: Clone\`

**URL:** <https://internals.rust-lang.org/t/implement-exactsizeiterator-for-zip-a-repeat-b-where-a-exactsizeiterator-b-clone/23537>\
**Category:** libs\
**Created:** [September 16, 2025, 11:35am UTC](https://internals.rust-lang.org/t/implement-exactsizeiterator-for-zip-a-repeat-b-where-a-exactsizeiterator-b-clone/23537 "2025-09-16T11:35:12Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![414owen](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/414owen/32/13621_2.png) [@414owen](https://internals.rust-lang.org/u/414owen)\
**Post date:** [September 16, 2025, 11:35am UTC](https://internals.rust-lang.org/t/implement-exactsizeiterator-for-zip-a-repeat-b-where-a-exactsizeiterator-b-clone/23537/1 "2025-09-16T11:35:12Z")

</div>

This is a proposal to add two instances:

```rust
impl<A, B> ExactSizeIterator for Zip<A, Repeat<B>>
where
    A: ExactSizeIterator,
    B: Clone,
{
}

impl<A, B> ExactSizeIterator for Zip<Repeat<A>, B>
where
    A: Clone,
    B: ExactSizeIterator,
{
}

```

This would appear to be completely safe. `.size_hint()` will take the minimum of `usize::MAX` (from `Repeat`), and the other lower bound.

I've added these instances to `core`, and added this coretest:

```rust
#[test]
fn zip_repeat_bounds() {
    let iter = [1, 2, 3].into_iter().zip(repeat(2));
    let (lower, upper) = iter.size_hint();
    assert_eq!(lower, 3);
    assert_eq!(upper, Some(3));
    assert_eq!(iter.len(), 3);

    let iter = repeat(2).zip([1, 2, 3].into_iter());
    let (lower, upper) = iter.size_hint();
    assert_eq!(lower, 3);
    assert_eq!(upper, Some(3));
    assert_eq!(iter.len(), 3);
}

```

Which passes.

There is currently only one implementation of `ExactSizeIterator` for `Zip`:

```rust
impl<A, B> ExactSizeIterator for Zip<A, B>
where
    A: ExactSizeIterator,
    B: ExactSizeIterator {}

```

Which doesn't overlap with the proposed instances, as `Repeat` does not implement `ExactSizeIterator`.

If this seems sensible, I'll add a tracking issue, and PR the changes upstream.

---

<div class="post-metadata">

**Author:** ![Jules-Bertholet](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/jules-bertholet/32/10671_2.png) [@Jules-Bertholet](https://internals.rust-lang.org/u/Jules-Bertholet)\
**Post date:** [September 16, 2025, 3:13pm UTC](https://internals.rust-lang.org/t/implement-exactsizeiterator-for-zip-a-repeat-b-where-a-exactsizeiterator-b-clone/23537/2 "2025-09-16T15:13:48Z")

</div>

> [@414owen](#):
>
> If this seems sensible, I'll add a tracking issue, and PR the changes upstream.

You don't need a tracking issue for adding a new trait impl, as they are insta-stable. Just PR it

---

<div class="post-metadata">

**Author:** ![414owen](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/414owen/32/13621_2.png) [@414owen](https://internals.rust-lang.org/u/414owen)\
**Post date:** [September 16, 2025, 3:15pm UTC](https://internals.rust-lang.org/t/implement-exactsizeiterator-for-zip-a-repeat-b-where-a-exactsizeiterator-b-clone/23537/3 "2025-09-16T15:15:20Z")

</div>

Thanks, I was hoping that was the case.

---

<div class="post-metadata">

**Author:** ![Vorpal](https://avatars.discourse-cdn.com/v4/letter/v/aca169/32.png) [@Vorpal](https://internals.rust-lang.org/u/Vorpal)\
**Post date:** [September 16, 2025, 5:06pm UTC](https://internals.rust-lang.org/t/implement-exactsizeiterator-for-zip-a-repeat-b-where-a-exactsizeiterator-b-clone/23537/4 "2025-09-16T17:06:42Z")

</div>

Why can't Repeat be exact size? It is known to always be infinite after all.

---

<div class="post-metadata">

**Author:** ![414owen](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/414owen/32/13621_2.png) [@414owen](https://internals.rust-lang.org/u/414owen)\
**Post date:** [September 16, 2025, 5:10pm UTC](https://internals.rust-lang.org/t/implement-exactsizeiterator-for-zip-a-repeat-b-where-a-exactsizeiterator-b-clone/23537/5 "2025-09-16T17:10:49Z")

</div>

What `usize` would you have `.len()` return for `Repeat`?

---

<div class="post-metadata">

**Author:** ![414owen](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/414owen/32/13621_2.png) [@414owen](https://internals.rust-lang.org/u/414owen)\
**Post date:** [September 16, 2025, 5:11pm UTC](https://internals.rust-lang.org/t/implement-exactsizeiterator-for-zip-a-repeat-b-where-a-exactsizeiterator-b-clone/23537/6 "2025-09-16T17:11:22Z")

</div>

PR is up: [Implement ExactSizeIterator for `Zip\<A, Repeat\<B\>\>` by 414owen · Pull Request #146642 · rust-lang/rust · GitHub](https://github.com/rust-lang/rust/pull/146642)

---

<div class="post-metadata">

**Author:** ![Vorpal](https://avatars.discourse-cdn.com/v4/letter/v/aca169/32.png) [@Vorpal](https://internals.rust-lang.org/u/Vorpal)\
**Post date:** [September 16, 2025, 6:07pm UTC](https://internals.rust-lang.org/t/implement-exactsizeiterator-for-zip-a-repeat-b-where-a-exactsizeiterator-b-clone/23537/7 "2025-09-16T18:07:21Z")

</div>

Ah, that is indeed a fair point. I only thought about size hint.

Edit: would adding a ExactInfinite trait make sense? Because the same logic could apply to other infinite iterators. Such as RepeatWith and probably several in the itertools crate.

---

<div class="post-metadata">

**Author:** ![kpreid](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/kpreid/32/8484_2.png) [@kpreid](https://internals.rust-lang.org/u/kpreid)\
**Post date:** [September 16, 2025, 6:11pm UTC](https://internals.rust-lang.org/t/implement-exactsizeiterator-for-zip-a-repeat-b-where-a-exactsizeiterator-b-clone/23537/8 "2025-09-16T18:11:26Z")

</div>

Could yo remove “[Pre-RFC]” from the title since this thread does not (and should not) contain an RFC draft?

---

<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:** [September 16, 2025, 7:35pm UTC](https://internals.rust-lang.org/t/implement-exactsizeiterator-for-zip-a-repeat-b-where-a-exactsizeiterator-b-clone/23537/9 "2025-09-16T19:35:06Z")

</div>

Trait impls can be unstable now, actually! I haven't tried it out but I'm almost positive that was merged.

---

<div class="post-metadata">

**Author:** ![tczajka](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/tczajka/32/8923_2.png) [@tczajka](https://internals.rust-lang.org/u/tczajka)\
**Post date:** [September 17, 2025, 6:25pm UTC](https://internals.rust-lang.org/t/implement-exactsizeiterator-for-zip-a-repeat-b-where-a-exactsizeiterator-b-clone/23537/10 "2025-09-17T18:25:34Z")

</div>

> [@Vorpal](#):
>
> would adding a ExactInfinite trait make sense? Because the same logic could apply to other infinite iterators.

Yes. But this would require a language feature that allows marking `InfiniteIterator` and `ExactSizeIterator` as mutually exclusive traits.

---

<div class="post-metadata">

**Author:** ![414owen](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/414owen/32/13621_2.png) [@414owen](https://internals.rust-lang.org/u/414owen)\
**Post date:** [September 23, 2025, 10:06am UTC](https://internals.rust-lang.org/t/implement-exactsizeiterator-for-zip-a-repeat-b-where-a-exactsizeiterator-b-clone/23537/11 "2025-09-23T10:06:15Z")

</div>

For those interested in getting instances of `ExactSizeIterator` for exactly-limited `infinite` iterator sources, I have an ACP out, and would appreciate feedback (or just a 👍, if you think this problem is worth solving): [Generalize ExactSizeIterator to QuantifiedIterator · Issue #659 · rust-lang/libs-team · GitHub](https://github.com/rust-lang/libs-team/issues/659)

tldr; in order to express the mutually exclusive traits, you can simply subsume both traits with one which can mark iterators as either infinite or exact (but not both, because it's an associated type, so can only be instantiated once per implementation).
