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.
This would be to
unique()whattake_while()is tofilter():iter.take_while_distinct()would yield items fromiteruntil it encounters the first duplicate, at which point it yieldsNone. I've needed this a few times and while it's not hard to implement myself, or even write an inline version,itertoolsalso seems like a good place for it.The type bounds should be the same as
unique():and the implementation would be equivalent to:
I'm not particularly attached to the name,
take_while_unique()orwhile_distinct()seem reasonable too.If this is desirable, I can write a PR.