improve worst-case performance of HashSet.is_subset

This commit is contained in:
Stein Somers 2019-04-03 13:01:01 +02:00
parent 546cb21f58
commit 5b8bfe0471

View file

@ -627,7 +627,11 @@ impl<T, S> HashSet<T, S>
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
pub fn is_subset(&self, other: &HashSet<T, S>) -> bool {
self.iter().all(|v| other.contains(v))
if self.len() <= other.len() {
self.iter().all(|v| other.contains(v))
} else {
false
}
}
/// Returns `true` if the set is a superset of another,