Skip to content
Kavachकवच

For Loop: With an index

● Beginner2 min

When you need each item's position too, loop over list.enumerate().

fn main() {
    const names = ["Asha", "Ravi", "Meena"]
    for (index, name) in names.enumerate() {
        print("{index}: {name}")
    }
}

The program ran and printed:

kavach run index.kav

0: Asha1: Ravi2: Meena
index.kav: Kavach code, then what running it prints

Positions start at 0. You get the position and the item together, with no counter of your own.