14. Map and Set
Map[K, V] and Set[T] are built-in persistent containers: every "modification"
returns a new container and leaves the original alone. There is no literal syntax; the
operations live in the std/map and std/set modules. Iteration order = insertion
order, on the JVM and on native alike.
use std/map
use std/set
pub fn main() -> Unit !io = {
let m = map.insert(map.insert(map.empty(), "a", 1), "b", 2)
println(to_string(map.get(m, "a")))
println(to_string(map.get(m, "z")))
println(to_string(map.keys(m)))
let s = set.from([3, 1, 2, 1, 3])
println(to_string(set.len(s)))
println(to_string(set.has(s, 2)))
}
Open in playground
Some(1)
None
["a", "b"]
3
true
A key may be of any type with structural equality (Int/String/tuples/ADTs/records).
map.get returns an Option[V] — a miss is None, not an exception. Equality ignores
order: two Maps with the same keys and values are equal.