Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 17 additions & 17 deletions exercises/custom-set/example.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ impl<T: Ord + Clone> PartialEq for CustomSet<T> {
}

impl<T: Ord + Clone> CustomSet<T> {
pub fn new(inputs: Vec<T>) -> CustomSet<T> {
pub fn new(inputs: &[T]) -> CustomSet<T> {
let mut s = CustomSet { collection: Vec::new() };
for input in inputs {
s.add(input);
s.add(input.clone());
}
s
}
Expand Down Expand Up @@ -42,26 +42,26 @@ impl<T: Ord + Clone> CustomSet<T> {
}

pub fn intersection(&self, other: &Self) -> CustomSet<T> {
CustomSet::new(self.collection
.iter()
.cloned()
.filter(|c| other.contains(c))
.collect())
CustomSet::new(&self.collection
.iter()
.cloned()
.filter(|c| other.contains(c))
.collect::<Vec<_>>())
}

pub fn union(&self, other: &Self) -> CustomSet<T> {
CustomSet::new(self.collection
.iter()
.cloned()
.chain(other.collection.iter().cloned())
.collect())
CustomSet::new(&self.collection
.iter()
.cloned()
.chain(other.collection.iter().cloned())
.collect::<Vec<_>>())
}

pub fn difference(&self, other: &Self) -> CustomSet<T> {
CustomSet::new(self.collection
.iter()
.cloned()
.filter(|c| !other.contains(c))
.collect())
CustomSet::new(&self.collection
.iter()
.cloned()
.filter(|c| !other.contains(c))
.collect::<Vec<_>>())
}
}
Loading