Lyricdev
GitHub

LYR-SEM0007

a value that cannot be indexed or iterated

error ยท spec 03-types

An index whose type is not int (or an integer type that widens to it), an index or range on a type that provides none, or a for over a value that is not an Iterable.

Explanation

a[i] works on arrays, slices and inline arrays, whose index is an int, and on types conforming to Index<K>, whose index is a K. A range a[i..j] takes a view of an array, a slice, a string, a StringView or a type with AsSlice<T>. for walks an Iterable, and every Iterator is one.

Convert the index with as int, or give the type the conformance it is used through.

Wrong

fn main(): void {
    let xs = [1, 2, 3];
    let _ = xs[true];
}
fn main(): void {
    let xs = [1, 2, 3];
    let _ = xs[1];
}