Skip to content
Kavachकवच

Maps: Reading a value

● Beginner2 min

map[key] gives an optional: the value, or none when the key isn't there.

fn main() {
    const ages = ["asha": 31]
    print(ages["asha"] ?? 0)
    print(ages["zoe"] ?? 0)
    if let age = ages["asha"] {
        print("found {age}")
    }
}

The program ran and printed:

kavach run read.kav

310found 31
read.kav: Kavach code, then what running it prints