E0009 — Array index out of bounds
An indexing operation asked for a position outside the array’s length.
What triggers it
Resilient arrays are bounds-checked on every index read and
write. A negative index or one >= len triggers this error at
runtime; no silent out-of-bounds reads, no undefined behaviour.
Minimal example
fn main() {
let xs = [1, 2, 3];
return xs[5];
}
Output:
scratch.rs:3:12: error[E0009]: array index 5 out of bounds for length 3
return xs[5];
^^^^^
Fix
Guard the index. len(xs) returns the current length; pair it
with a runtime check or, better, a requires i < len(xs)
contract so the bound is lifted to compile time.
fn main() {
let xs = [1, 2, 3];
let i = 1;
if i >= 0 && i < len(xs) {
return xs[i];
}
return 0;
}
Source
Emitted from the interpreter’s IndexExpression arm, the VM
(Op::LoadIndex / Op::StoreIndex in resilient/src/vm.rs
— produces VmError::ArrayIndexOutOfBounds), and the JIT via
the res_array_get / res_array_set shims in
resilient/src/jit_backend.rs.