Ruta graveolens  ·  notes from a language experiment  ·  cultivated since 2025

E0481: Invalid array length

CodeCategoryStability
E0481Struct and enumPermanent

Explanation

An array length must be a non-negative integer value known at compile time. Integer literals, annotated integer constants, comptime parameters, and comptime-evaluable function calls may provide the length; runtime variables, undefined names, non-integer values, and negative results cannot.

Likely cause

The expression after the semicolon in [T; N] depends on runtime state, resolves to a value other than a non-negative integer, or names a missing or unannotated constant. Replace it with a valid compile-time integer expression and annotate any value constant it uses.

Examples

Use a negative array length

const LENGTH: i32 = -1;
fn main() -> i32 {
    let values: [i32; LENGTH] = [];
    0
}

Use an annotated non-negative constant

const LENGTH: i32 = 2;
fn main() -> i32 {
    let values: [i32; LENGTH] = [20, 22];
    values[0] + values[1]
}

References