Skip to content

std/set ​

Hash set collection for unique values. Method-based on the HashSet<T> value.

milo
from "std/set" import { HashSet }

Example ​

milo
var seen = HashSet<string>.new()
seen.add("alice")
seen.add("bob")
seen.add("alice")  // no-op
print(intToString(seen.len()))  // 2

API reference ​

HashSet ​

milo
pub struct HashSet<T>

An unordered collection of unique values.

HashSet.add ​

milo
fn HashSet.add(self: &mut HashSet<T>, val: T): void

Add a value to the set.

No-op if the value is already present.

HashSet.clear ​

milo
fn HashSet.clear(self: &mut HashSet<T>): void

Drop every element, keeping the table's capacity.

HashSet.clone ​

milo
fn HashSet.clone(self: &HashSet<T>): HashSet<T>

Element by element rather than self.inner.clone(): the builtin map clone copies each key structurally and never runs T's own Clone impl, so a T carrying Drop was released once per copy (the generic-drop fuzzer's HashSet.clone finding). Same reason every other constructor here spells x.clone().

HashSet.contains ​

milo
fn HashSet.contains(self: &HashSet<T>, val: T): bool

Check if the set contains a value.

HashSet.difference ​

milo
fn HashSet.difference(self: &HashSet<T>, other: &HashSet<T>): HashSet<T>

Everything in this set that is not in other.

HashSet.fromVec ​

milo
fn HashSet.fromVec(items: Vec<T>): HashSet<T>

Collect a Vec into a set, dropping duplicates.

HashSet.intersect ​

milo
fn HashSet.intersect(self: &HashSet<T>, other: &HashSet<T>): HashSet<T>

Everything in both sets.

HashSet.isEmpty ​

milo
fn HashSet.isEmpty(self: &HashSet<T>): bool

HashSet.isSubsetOf ​

milo
fn HashSet.isSubsetOf(self: &HashSet<T>, other: &HashSet<T>): bool

True when every element of this set is also in other.

HashSet.len ​

milo
fn HashSet.len(self: &HashSet<T>): i64

Number of elements in the set.

HashSet.new ​

milo
fn HashSet.new(): HashSet<T>

Create an empty HashSet.

A generic static takes its type argument: HashSet<string>.new().

HashSet.remove ​

milo
fn HashSet.remove(self: &mut HashSet<T>, val: T): void

Remove a value from the set.

No-op if the value is absent.

HashSet.toVec ​

milo
fn HashSet.toVec(self: &HashSet<T>): Vec<T>

Snapshot the elements into a Vec. Sort it if you need a stable order — print on a set would otherwise expose the randomized bucket order. Not self.inner.keys(), for the reason clone gives.

HashSet.union ​

milo
fn HashSet.union(self: &HashSet<T>, other: &HashSet<T>): HashSet<T>

Everything in either set.

HashSet.withCapacity ​

milo
fn HashSet.withCapacity(capacity: i64): HashSet<T>

Create an empty HashSet sized so capacity adds never rehash.