std/set
Import with import std/set.
Exports
Section titled “Exports”Types
struct Set
Section titled “struct Set”struct Set<'T>
opaque — construction surface is private to its defining module
Immutable collection of unique T values.
Set.new
Section titled “Set.new”fn new(): Set<'T>
type function on Set
The empty set.
Interactive Tests
s: Set<Int> = Set.new()
assert Set.size(s) == 0
Set.from_list
Section titled “Set.from_list”fn from_list(elems: List<'T>): Set<'T>
type function on Set
Builds a set from a list, discarding duplicates (first occurrence wins for
ordering). Prefer the #{...} literal when the elements are known inline.
Interactive Tests
assert Set.from_list([1, 2, 2, 3]) == #{1, 2, 3}
Set.insert
Section titled “Set.insert”fn insert(s: Set<'T>, elem: 'T): Set<'T>
type function on Set
Returns a new set with elem added (no-op if already present).
Interactive Tests
assert Set.insert(#{1, 2}, 3) == #{1, 2, 3}
assert Set.insert(#{1, 2}, 2) == #{1, 2}
Set.remove
Section titled “Set.remove”fn remove(s: Set<'T>, elem: 'T): Set<'T>
type function on Set
Returns a new set with elem removed (no-op if absent).
Interactive Tests
assert Set.remove(#{1, 2, 3}, 2) == #{1, 3}
assert Set.remove(#{1, 2}, 9) == #{1, 2}
Set.contains?
Section titled “Set.contains?”fn contains?(s: Set<'T>, elem: 'T): Bool
type function on Set
True if elem is a member of the set.
Interactive Tests
assert Set.contains?(#{1, 2}, 1)
refute Set.contains?(#{1, 2}, 9)
Set.size
Section titled “Set.size”fn size(s: Set<'T>): Int
type function on Set
The number of elements. O(1) — the set’s own count; the generic count is
the Iter.count module function.
Interactive Tests
assert Set.size(#{1, 2, 3}) == 3
Set.union
Section titled “Set.union”fn union(a: Set<'T>, b: Set<'T>): Set<'T>
type function on Set
All elements in either set.
Interactive Tests
assert Set.union(#{1, 2}, #{2, 3}) == #{1, 2, 3}
Set.intersection
Section titled “Set.intersection”fn intersection(a: Set<'T>, b: Set<'T>): Set<'T>
type function on Set
Elements in both sets.
Interactive Tests
assert Set.intersection(#{1, 2}, #{2, 3}) == #{2}
Set.difference
Section titled “Set.difference”fn difference(a: Set<'T>, b: Set<'T>): Set<'T>
type function on Set
Elements in a but not b.
Interactive Tests
assert Set.difference(#{1, 2, 3}, #{2}) == #{1, 3}
Set.subset?
Section titled “Set.subset?”fn subset?(a: Set<'T>, b: Set<'T>): Bool
type function on Set
True if every element of a is also in b.
Interactive Tests
assert Set.subset?(#{1, 2}, #{1, 2, 3})
refute Set.subset?(#{1, 4}, #{1, 2, 3})
Set.next
Section titled “Set.next”impl Iterable
fn next(s: Set<'T>): Maybe<('T, Set<'T>)>
impl Iterable.next
Returns the next element and the remaining set, or None when empty.
Elements are yielded in insertion order (matching to_list). Delegates to
the backing map’s iterator, so each step is O(1).
Set.known_count
Section titled “Set.known_count”impl Iterable
fn known_count(s: Set<'T>): Maybe<Int>
impl Iterable.known_count
Set.to_string
Section titled “Set.to_string”impl Display
fn to_string(s: Set<'T>): String
impl Display.to_string
Set.inspect
Section titled “Set.inspect”impl Debug
fn inspect(s: Set<'T>): String
impl Debug.inspect
Set.equal?
Section titled “Set.equal?”impl Equatable
fn equal?(a: Set<'T>, b: Set<'T>): Bool
impl Equatable.equal?
Set.hash
Section titled “Set.hash”impl Hashable
fn hash(s: Set<'T>): Int
impl Hashable.hash