For Loop: Over a map
● Beginner3 min
Loop over a map: each round gives a key and its value together.
fn main() {
const ages = ["asha": 31, "ravi": 27]
for (name, age) in ages {
print("{name} is {age}")
}
}The program ran and printed:
kavach run map.kav
asha is 31ravi is 27
(name, age) takes each entry apart into its two halves. The entries come in the order they were added, every time.
What Kavach does for you
Maps and sets keep their order
Looping over a map visits entries in the order they were added, so a program prints the same thing every run, and tests stay repeatable.