Missing methods for sets and maps


Two groups of methods that I think are missing:

  • When one considers sets one generally want to be able to compare them from the view point of inclusion. While it’s currently possible to compare sets using is_disjoint, is_subset and is_superset, those methods cover too restricted cases (you cannot know if a set is a proper superset for example, except by additionally checking if both sets are unequal). More generally I think it would be nice to have a enum representing the different kind of set relations and a trait comparing two sets:
pub enum SetComparison {
    Overlapping, // the intersection and both set differences contain elements

pub trait SetRel: Eq {
    fn set_cmp(&self, other: &Self) -> SetComparison;

Notice that this allows plenty of more specific comparison relations (for example to be a non-proper subset is simply to return either Equal or ProperSubset). Of course such a trait can be implemented externally but as such it may not benefit from the best implementation.

  • As Java shows it, there exists interesting additional methods for BTreeSet ( respectively BTreeMap) : the capacity to retrieve not precisely a given element (respectively a given key-value pair) but the closest (strictly) smaller/bigger element (respectively key-value pair) if any. Even better: one may want to have partial views that represent left/right/inner “intervals”. Once again this may be implemented externally but not as efficiently as if one can access the inner details of such types.

I think the only additional check you need is if the lengths are unequal – which you might even check first since that’s trivial. If one is larger and a superset, then it’s a proper superset.

In fact, the HashSet::eq() implementation is basically that the lengths are equal and one is a superset.

1 Like

You’re correct. I guess I was thinking about how C# is handling this. The second argument of comparison operations is not another set but more generally an IEnumerable (the equivalent of Rust’s IntoIterator). This allows you to determine how an arbitrary sequence of items compares to a given set. But in this case no length is available.

closed #4

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.