Skip to content

std/set

Import with import std/set.

Types

struct Set<'T>

opaque — construction surface is private to its defining module

Immutable collection of unique T values.

fn new(): Set<'T>

type function on Set

The empty set.

Interactive Tests

s: Set<Int> = Set.new()
assert Set.size(s) == 0
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}
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}
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}
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)
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
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}
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}
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}
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})

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).

impl Iterable

fn known_count(s: Set<'T>): Maybe<Int>

impl Iterable.known_count

impl Display

fn to_string(s: Set<'T>): String

impl Display.to_string

impl Debug

fn inspect(s: Set<'T>): String

impl Debug.inspect

impl Equatable

fn equal?(a: Set<'T>, b: Set<'T>): Bool

impl Equatable.equal?

impl Hashable

fn hash(s: Set<'T>): Int

impl Hashable.hash