Bounds Checking

Array access with an out-of-bounds index MUST cause a runtime panic.

On out-of-bounds access, the program MUST terminate with exit code 101 and print an error message.

For constant indices, bounds checking MUST be performed at compile time.

A constant index is an expression that can be fully evaluated at compile time. This includes integer literals, arithmetic operations on constants, comparison operations on constants, and parenthesized constant expressions.

For variable indices, bounds checking MUST be performed at runtime before the access.

fn main() -> i32 {
    let arr: [i32; 3] = [1, 2, 3];
    let idx: u64 = 10;
    arr[idx]  // Runtime error: index out of bounds
}
fn main() -> i32 {
    let arr: [i32; 3] = [1, 2, 3];
    arr[5]  // Compile-time error: index out of bounds
}
fn main() -> i32 {
    let arr: [i32; 3] = [1, 2, 3];
    arr[1 + 5]  // Compile-time error: index out of bounds
}