Skip to content

set

The set module provides an unordered collection of unique elements, backed by HashMap for O(1) lookup.

from std::core::set use {
new, from_array, add, remove, includes, union,
intersection, difference, to_array, len
}
let empty = new()
let s = from_array(["a", "b", "c", "b", "a"]) // deduplicates → {"a","b","c"}

Note: at HEAD 0c56ab86, the underlying HashMap rejects non-string keys. Until W17.3-4.3-adjacent HashMap-key-kind work lands, sets of int or other non-string elements will surface HashMap key must be a string (got kind ...) at runtime.

let s = from_array(["a", "b", "c"])
let s2 = add(s, "d")
let s3 = remove(s2, "b")
print(includes(s3, "a")) // true
print(includes(s3, "b")) // false
print(len(s3)) // 3
print(to_array(s3)) // ["a", "c", "d"]
let a = from_array(["x", "y", "z"])
let b = from_array(["y", "z", "w"])
let u = union(a, b) // {"x","y","z","w"}
let i = intersection(a, b) // {"y","z"}
let d = difference(a, b) // {"x"}
FunctionDescription
new()Create an empty set
from_array(arr)Create a set from an array (deduplicates)
add(s, item)Return new set with item added
remove(s, item)Return new set with item removed
includes(s, item)Check if set contains item
union(a, b)Union of two sets
intersection(a, b)Intersection of two sets
difference(a, b)Difference (a minus b)
to_array(s)Convert set to array
len(s)Number of elements

Sets are immutableadd, remove, and set algebra functions return new sets.