Skip to content

Feature request: take_while_distinct() adapter #1119

Description

@ronnodas

This would be to unique() what take_while() is to filter(): iter.take_while_distinct() would yield items from iter until it encounters the first duplicate, at which point it yields None. I've needed this a few times and while it's not hard to implement myself, or even write an inline version, itertools also seems like a good place for it.

The type bounds should be the same as unique():

Self::Item : Clone + Eq + Hash

and the implementation would be equivalent to:

let mut seen = HashSet::new();
iter.take_while(|item| seen.insert(item.clone()))

I'm not particularly attached to the name, take_while_unique() or while_distinct() seem reasonable too.

If this is desirable, I can write a PR.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions