LEARN VOID / DATA & BEHAVIOR
Dictionaries
Associate keys and values while preserving insertion order.
Complete program for the synchronous entry adapter. The result below is the returned integer.
func entry(extra_notebooks: i32) -> i32:
var supplies: {string: i32} = {"notebooks": 2, "pens": 5}
supplies["notebooks"] += extra_notebooks
return match supplies.get("notebooks"):
Ok(var quantity): quantity
Err(var reason): 0Create a typed mapping
{string: i32} maps supply names to quantities. The literal starts with two notebooks and five pens. Looking up supplies["notebooks"] yields the quantity for that known key.
Update and insert
Compound assignment adds the input to an existing notebook quantity; input 5 gives 7. Assigning a value to a new key inserts it. The program uses get("notebooks") to handle lookup as a result. Direct indexing and compound assignment require the key to exist; missing keys raise a runtime failure. Use contains(key) to test presence, or get(key) to obtain Ok(value) or Err("key not found"). This example chooses zero if the key is missing.
Explore membership and order
Iteration uses for quantity, name in supplies: in insertion order; reverse visits the reverse order. Replacing a value preserves its key’s position. remove(key) reports whether removal happened, while clear() empties the mapping. Assignment shares dictionary identity. Recoverable lookup preserves the value type and its readonly restrictions.