Repository: rust
Issue: Tracking Issue for Iterator::{dedup, dedup_by, dedup_by_key}
Labels: T-libs-api,C-tracking-issue,A-iterators

<!--
Thank you for creating a tracking issue!

Tracking issues are for tracking a feature from implementation to stabilization.

Make sure to include the relevant RFC for the feature if it has one.

If the new feature is small, it may be fine to skip the RFC process. In that
case, you can use use `issue = "none"` in your initial implementation PR. The
reviewer will ask you to open a tracking issue if they agree your feature can be
added without an RFC.
-->

Feature gate: `#![feature(iter_dedup)]`

This is a tracking issue for the functions `std::iter::Iterator::{dedup, dedup_by, dedup_by_key}` as well as the structs `std::iter::{Dedup, ByKey, ByPartialEq}`.

This feature provides similar functionality as the functions for slices and `Vec`s with the same name:

```rust
let vec = vec![1, 2, 2, 3, 2];

let mut iter = vec.into_iter().dedup();

assert_eq!(iter.next(), Some(1));
assert_eq!(iter.next(), Some(2));
assert_eq!(iter.next(), Some(3));
assert_eq!(iter.next(), Some(2));
assert_eq!(iter.next(), None);
```

<!--
Include a short description of the feature.
-->

### Public API

<!--
For most library features, it'd be useful to include a summarized version of the public API.
(E.g. just the public function signatures without their doc comments or implementation.)
-->

```rust
pub trait Iterator {
    /// Removes all but the first of consecutive elements in the iterator
    /// according to the `PartialEq` trait implementation.
    fn dedup(self) -> Dedup<Self, ByPartialEq>
    where
        Self: Sized,
        Self::Item: PartialEq,
    { ... }
    
    /// Removes all but the first of consecutive elements in the iterator
    /// satisfying a given equality relation.
    fn dedup_by<F>(self, same_bucket: F) -> Dedup<Self, F>
    where
        Self: Sized,
        F: FnMut(&Self::Item, &Self::Item) -> bool,
    { ... }
    
    /// Removes all but the first of consecutive elements in the iterator
    /// that resolve to the same key.
    fn dedup_by_key<F, K>(self, key: F) -> Dedup<Self, ByKey<F>>
    where
        Self: Sized,
        F: FnMut(&Self::Item) -> K,
        K: PartialEq,
    { ... }
}
```

### Steps / History

<!--
For larger features, more steps might be involved.
If the feature is changed later, please add those PRs here as well.
-->

- [ ] Implementation: #83748
- [ ] Final commenting period (FCP)
- [ ] Stabilization PR

<!--
Once the feature has gone through a few release cycles and there are no
unresolved questions left, the feature might be ready for stabilization.

If this feature didn't go through the RFC process, a final commenting period
(FCP) is always needed before stabilization. This works as follows:

A library team member can kick off the stabilization process, at which point
the rfcbot will ask all the team members to verify they agree with
stabilization. Once enough members agree and there are no concerns, the final
commenting period begins: this issue will be marked as such and will be listed
in the next This Week in Rust newsletter. If no blocking concerns are raised in
that period of 10 days, a stabilzation PR can be opened by anyone.
-->

### Unresolved Questions

<!--
Include any open questions that need to be answered before the feature can be
stabilised. If multiple (unrelated) big questions come up, it can be a good idea
to open a separate issue for each, to make it easier to keep track of the
discussions.

It's useful to link any relevant discussions and conclusions (whether on GitHub,
Zulip, or the internals forum) here.
-->

- ~~Is there a way to reduce `dedup` and `dedup_by_key` to a call to `dedup_by` without creating an impossible function signature?~~
- ~~Should we also implement `SourceIter` and `InPlaceIterable` for these structs?~~
- ~~How should/can we react to an infinite iterator where all items are the same?~~
- ~~Is there a way to turn this into a no-op for iterators which cannot contain duplicates (e.g. `hashset.iter().dedup()`)?~~