rust-lib/src/vector/distinct.rs
Martin Berg Alstad d5974dda20 Initial commit
2024-06-22 17:19:55 +02:00

29 lines
577 B
Rust

#[cfg(feature = "vec")]
pub trait Distinct {
fn distinct(&mut self);
}
#[cfg(feature = "vec")]
impl<T: PartialEq + Clone> Distinct for Vec<T> {
fn distinct(&mut self) {
*self = self.iter().fold(vec![], |mut acc, x| {
if !acc.contains(x) {
acc.push(x.clone());
}
acc
});
}
}
#[cfg(all(test, feature = "vec"))]
mod tests {
use super::*;
#[test]
fn test_distinct() {
let mut vec = vec![1, 2, 3, 1, 2, 3];
vec.distinct();
assert_eq!(vec, vec![1, 2, 3]);
}
}