Map
Maps are similar to arrays except that you can have a custom key type, instead of it being an integer. The syntax for declaring a map is [<key type>]<value type>. So, for example, a map with a string key and a rational value would be declared as:
[str]rat myMap = [
"string1": 1.0,
"string2": 2.0,
// ...
"string10": 10.0,
]
Trailing commas are allowed in map declarations. Similar to arrays, you can access the length of the map with <map>.len. Values of a map can be accessed with <map>[<key>]. For example, for the map above, we can access the value corresponding to "string5" with:
myMap["string5"] // 5.0
If the map is mutable, you can write to the map using the same syntax:
mut [str]rat myMap = [
// same as above
]
myMap["string11"] = 11.0
You can use a for loop to loop through the keys and values of a map:
for key in myMap {
print("{key} {myMap[key]}")
}
// string1 1.0
// string2 2.0
// string3 3.0
// string4 4.0
// string5 5.0
// string6 6.0
// string7 7.0
// string8 8.0
// string9 9.0
// string10 10.0
// string11 11.0
Maps are implemented using Swiss tables for high read/write performance. Note that the implementation may change later, and so you should generally not rely on the order that the entries are stored in, or the implementation details in any way.