From 5b8bfe047123bad63ead0370c165cd9307a07caa Mon Sep 17 00:00:00 2001 From: Stein Somers Date: Wed, 3 Apr 2019 13:01:01 +0200 Subject: [PATCH] improve worst-case performance of HashSet.is_subset --- src/libstd/collections/hash/set.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/libstd/collections/hash/set.rs b/src/libstd/collections/hash/set.rs index 89d5b2ff30f9f..b9fcc2365fa7c 100644 --- a/src/libstd/collections/hash/set.rs +++ b/src/libstd/collections/hash/set.rs @@ -627,7 +627,11 @@ impl HashSet /// ``` #[stable(feature = "rust1", since = "1.0.0")] pub fn is_subset(&self, other: &HashSet) -> 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,