set
The set module provides an unordered collection of unique elements, backed by HashMap for O(1) lookup.
Import
Section titled “Import”from std::core::set use { new, from_array, add, remove, includes, union, intersection, difference, to_array, len}Creating Sets
Section titled “Creating Sets”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.
Operations
Section titled “Operations”let s = from_array(["a", "b", "c"])
let s2 = add(s, "d")let s3 = remove(s2, "b")
print(includes(s3, "a")) // trueprint(includes(s3, "b")) // falseprint(len(s3)) // 3print(to_array(s3)) // ["a", "c", "d"]Set Algebra
Section titled “Set Algebra”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"}Functions
Section titled “Functions”| Function | Description |
|---|---|
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 immutable — add, remove, and set algebra functions return new sets.