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 keylet alice_age = ages["alice"]access value at key
for (name, age) in ages {order changes between runs fmt.Println(name, age)}Map.new()
Section titled “Map.new()”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"] = 30Map.from()
Section titled “Map.from()”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)])Map.length()
Section titled “Map.length()”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()2Map.is_empty()
Section titled “Map.is_empty()”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()trueMap.get()
Section titled “Map.get()”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 0Map.delete()
Section titled “Map.delete()”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 entryMap.clone()
Section titled “Map.clone()”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()Map.equals()
Section titled “Map.equals()”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