Array Types
An array type, written [T; N], represents a fixed-size sequence of N elements of type T.
The length N MUST be a non-negative integer literal known at compile time.
All elements of an array MUST have the same type T.
Arrays are stored contiguously in memory. The size of [T; N] is N * size_of(T). Zero-length arrays [T; 0] are zero-sized types. See Zero-Sized Types for the general definition.
fn main() -> i32 {
let arr: [i32; 3] = [10, 20, 12];
arr[0] + arr[1] + arr[2] // 42
}
Array elements are accessed using index syntax arr[index].
For constant indices, bounds checking is performed at compile time.
For variable indices, bounds checking is performed at runtime.
Accessing an array with an out-of-bounds index causes a runtime panic.
fn main() -> i32 {
let arr: [i32; 3] = [1, 2, 3];
let idx = 5;
arr[idx] // Runtime error: index out of bounds
}