Skip to content

Map

An unordered collection of key-value pairs. Equivalent to Go’s map[K]V. Keys must be comparable.

let mut ages = Map.new<string, int>()
ages["alice"] = 30set value at key
let alice_age = ages["alice"]access value at key
for (name, age) in ages {
order changes between runs
fmt.Println(name, age)
}

Creates an empty Map.

impl<K, V> Map<K, V> {
fn new() -> mut Map<K, V>
}
let mut ages = Map.new<string, int>()
ages["alice"] = 30

Creates a Map from a Slice of key-value pairs.

impl<K, V> Map<K, V> {
fn from(pairs: Slice<(K, V)>) -> mut Map<K, V>
}
let ages = Map.from([("alice", 30), ("bob", 25)])

Returns the number of entries in the Map.

impl<K, V> Map<K, V> {
fn length(self) -> int
}
let ages = Map.from([("alice", 30), ("bob", 25)])
let entries = ages.length()2

Returns true if the Map holds no entries.

impl<K, V> Map<K, V> {
fn is_empty(self) -> bool
}
let ages = Map.new<string, int>()
let blank = ages.is_empty()true

Returns the value for key, or None if not present. Reading a missing key with [] gives the value type’s zero value, so use get() to tell a miss from a stored zero.

impl<K, V> Map<K, V> {
fn get(self, key: K) -> Option<V>
}
let ages = Map.from([("alice", 30)])
let age = ages.get("alice")Some(30)
let missing = ages.get("bob")None, while ages["bob"] gives 0

Removes the entry for key from the Map.

impl<K, V> Map<K, V> {
fn delete(self: mut Map<K, V>, key: K)
}
let mut ages = Map.from([("alice", 30), ("bob", 25)])
ages.delete("alice")
leaves bob as the only entry

Returns a copy of the Map, including any nested slices, maps, and tuples.

impl<K, V> Map<K, V> {
fn clone(self) -> mut Map<K, V>
}
let ages = Map.from([("alice", 30)])
let copy = ages.clone()

Returns true if this Map has the same keys and values as other.

impl<K, V> Map<K, V> {
fn equals(self, other: Map<K, V>) -> bool
}
let first = Map.from([("alice", 30)])
let second = Map.from([("alice", 30)])
first.equals(second)true