map
A map is a collection of key-value pairs, where each key is unique and maps to a corresponding value. Maps are useful for storing and retrieving data based on keys.
Syntax
You can define maps using curly braces {} with a colon : to separate keys from values.
{"a": 1} -- A single key-value pair
{"a": 10, "b": 20} -- Multiple key-value pairs
{"a": {"b": 2}} -- Nested maps
{
"a": 10
"b": 20
"c": 30
} -- Multi-line format
Evaluates to:
A single key-value pair
{"a":10, "b":20}--Multiple key-value pairs
{"a":{"b":2}}--Nested maps
{"a":10, "b":20, "c":30}--Multi-line format
Kind
The kind of a map describes the type of its keys and values:
<{K:V}>Where K is the kind of the keys, and V is the kind of the values. For example:
<{string:u8}>--A map from strings to unsigned 8-bit integers
<{string:{string:u8}}>--A nested map
<{u8:f64}>--A map from unsigned 8-bit integers to 64-bit floats, can only hold 256 keys
Construction
Maps can be constructed using map literals.
Map Literals
Accessing Elements
Elements in a map are accessed using square brackets [] with the key.
Returns 10
If the key does not exist, the result is none.
For nested maps, use multiple brackets:
Returns 2
Assigning Elements
Maps can be modified if they are declared mutable with ~.
Updates key "a"
m{"b"} = 17 --Adds key "b"
m