Skip to content
Kavachकवच

Lists: Changing items

● Beginner2 min

list[i] = value replaces an item. The list must be a let.

fn main() {
    let scores = [70, 85, 90]
    scores[1] = 88
    print(scores)
}

The program ran and printed:

kavach run set-item.kav

[70, 88, 90]
set-item.kav: Kavach code, then what running it prints

Changing a const list is refused, with the fix:

fn main() {
    const fixed = [1, 2]
    fixed.push(3)
    print(fixed)
}

The compiler refused the program:

kavach run const-push.kav

error: `push` changes `fixed`, but it is a `const`: declare it with `let`
const-push.kav: Kavach code, then what running it prints