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

E0901: Array length mismatch

CodeCategoryStability
E0901ArrayPermanent

Explanation

An array type carries its length, so [i32; 3] and [i32; 2] are different types (3.5:1). E0901 reports a disagreement about that length and names both, expected first. It is raised in two places: an array literal whose element count differs from the length its context demands (7.1:4), and unification of two array types that agree on the element type but not on the length. A context that supplies the type also supplies the length — a let annotation, a parameter type, a return type — and the literal must then have exactly that many elements; with no such context the literal's own element count fixes the type, so the disagreement can only surface where two lengths meet. A literal whose elements disagree about the element type is a different failure: that is a type mismatch (E0206) at the offending element.

Likely cause

An element was added to or removed from an array literal without updating the declared length, or a literal was written against the wrong annotation. Make the element count match the declared length, or change the annotation to the literal's length. A count that varies at run time is not an array length at all: array lengths are compile-time constants (3.5:2), so a run of elements whose size is not known until run time is passed as a borrowed slice [T] instead.

Examples

An array literal shorter than its annotation

fn main() -> i32 {
    let xs: [i32; 3] = [10, 20];
    xs[0]
}

Match the declared length

fn main() -> i32 {
    let xs: [i32; 3] = [10, 20, 30];
    xs[0]
}

References