Array index expressions
ArrayIndexExpression = ExpressionWithoutBlock , "[" , ExpressionWithoutBlock , "]"
;
An array index expression evaluates the operand and the index expression and
produces the element of the array at that index. The
index expression must be a u16.
local-array preview feature
Warning
Requires
local-arraypreview feature enabled.
Indexing a local variable at runtime requires the
local-array preview feature to be enabled:
global A: [u8; 2];
fn main() {
let a = A;
let first: u8 = a[0]; // requires `local-array`
}
The preview feature is not required when the indexed operand is a global variable or when it appears inside a const item:
global A: [u8; 2];
const SECOND: u8 = {
let a: [u8; 2] = [1, 2];
yield a[1]; // ok, evaluated at compile time
};
fn main() {
let first: u8 = A[0]; // ok, index on a global
}