Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Things on this page are fragmentary and immature notes/thoughts of the author. Please read with your own judgement!

Tips and Traps

  1. Summary of Collections in Rust has a good summary on when to each which collection in Rust.

  2. Struct std::collections::HashSet has a good discussion on implementation details on HashSet and things to note when use it.

  3. BTreeSet is an alternative B-Tree-based implementation to HashSet. Elements in BTreeSet are sorted while elements in a HashSet are not sorted.

  4. TinySet provides a few collections that are optimized to scale in size well for small numbers of elements, while still scaling well in time (and size) for numbers of elements.

capacity

Creates an empty HashSet with the specified capacity. The hash set will be able to hold at least capacity elements without reallocating. If capacity is 0, the hash set will not allocate.

7
28

clear

{}

contains

true
false

difference

s1 - s2: {1, 0, 2}
s1: {0, 2, 3, 1, 4}
()
s1.difference(s2): [0, 2, 1]
s1: {0, 2, 3, 4, 1}
()

get

Some(1)
None

get_or_insert

    let v = s.get_or_insert(5);
              ^^^^^^^^^^^^^ 
use of unstable library feature 'hash_set_entry'

insert

{4, 100, 0, 1, 2, 3}

intersection

[3, 4]

is_disjoin

false
true

replace

This is the same as the function get_or_insert.

{2, 4, 0, 3, 1}
{2, 3, 0, 100, 4, 1}

BTreeSet as An Alternative to HashSet

BTreeSet is implemented based on B-Tree (binary search tree with each node containing a continuous array to improve caching performance). The main feature of BTreeSet compared to HashSet is that elements in a BTreeSet are sorted!

{0, 1, 2, 3, 4}
{0, 1, 2, 3, 4}