Map
A map is a collection of key-value pairs. Each key is unique in the map, and all keys are associated with a value. It is a mutable collection.
An expression like {"key1": value1, "key2": value2}
represents a map, called a map literal.
If the key and value types of the map are basic types (Int
, String
,Bool
, Double
, etc.),
then the map can be written as a map literal.
In other cases, you can create the map using the Map::of
function. It takes an array of two-element tuples, where the first element is the key and the second element is the value.
Values in a map can be accessed by the key using the map[key]
syntax.
The elements in a map can be updated using the syntax: map[key] = new_value
.
fn main {
// Create a map by map literal
let map1 = { "key1": 1, "key2": 2, "key3": 3 }
println(map1)
// You can also create a map by Map::of, from a list of key-value pairs
let map2 = Map::of([("key1", 1), ("key2", 2), ("key3", 3)])
println(map1 == map2)
// Access a value by key
println(map1["key1"])
// Update a value by key
map1["key1"] = 10
println(map1)
// test a if a key exists
println(map1.contains("key1"))
}