unique_rust/lib.rs
1pub trait Unique<T> {
2 fn unique<P>(&mut self, pred: P) -> Option<T>
3 where
4 P: FnMut(&T) -> bool;
5}
6
7impl<T, I> Unique<T> for I
8where
9 I: Iterator<Item = T>,
10{
11 fn unique<P>(&mut self, mut pred: P) -> Option<T>
12 where
13 P: FnMut(&T) -> bool,
14 {
15 let mut val = self.filter(|x| pred(x));
16 let value = val.next();
17 let value_next = val.next();
18 match value_next {
19 Some(_) => None,
20 None => value,
21 }
22 }
23}