Skip to content

first_duplicate

Returns the first item of iter that has already appeared earlier in it, or None when every item is unique.

Unlike array::duplicates, this stops at the first repeat, so it works on an infinite or otherwise unbounded iterator instead of requiring an already-collected slice.

use helpers4::iter::first_duplicate;

Cargo feature iter (enabled by default). To compile only this module:

cargo add helpers4 --no-default-features --features iter

or in Cargo.toml:

[dependencies]
helpers4 = { version = "0.0.5", default-features = false, features = ["iter"] }
pub fn first_duplicate<T: Eq + Hash + Clone>(iter: impl IntoIterator<Item = T>) -> Option<T>
ParameterTypeDescription
iterimpl IntoIterator<Item = T>The items to scan, in order.

Option<T> — The first repeated item, or None when there is none.

use helpers4::iter::first_duplicate;

assert_eq!(first_duplicate([1, 2, 3, 2, 1]), Some(2));
assert_eq!(first_duplicate(["a", "b", "c"]), None);

src/iter/first_duplicate.rs