Skip to content

custom-set: Use &[...] instead of vec![...] #457

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 4, 2018
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