Sets containers in Rust

I want to create a Set of numbers, that I can insert integers into the set, only keep the unique ones, count their numbers, and determine the largest value in the set. Looking in the docs this was the only thing I found. Is this what I should use or is there something better (in std lib or crate) for this task?

Given this requirement, a BTreeSet should be more suitable: since the contents of a BTreeSet are always sorted, it is easy to extract a minimum (.iter().copied().next()) or a maximum (.iter().rev().copied().next()).

If the largest is expected to be relatively small, a bit vector or bit set may be useful. There's no such container in the standard library, but there are crates available, e.g. https://crates.io/crates/bit-set

1 Like

If you want to count the instances of each number, a regular HashMap makes more sense. As for determining the largest, just do that in a separate variable.

1 Like

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