# Arrays

An array `[T; N]` stores a fixed number of elements of one type. The count is
part of the type, so `[i32; 3]` and `[i32; 4]` are different types. Use an array
when a shape is fixed; use `Vec<T>` when the sequence must grow or shrink.

## Declaration and initialization

```vex
let samples: [i32; 4] = [10, 20, 30, 40];
let zeros: [i32; 4] = [0; 4];
let weights: [f32; 3] = [1.0, 2.0, 3.0];
```

A repeated initializer is useful for Copy values such as numbers. It does not
mean that an arbitrary owning value can be duplicated by copying its bytes.

## A complete computation

```vex [array-sum.vx]
fn sum4(values: [i32; 4]): i32 {
    let! total = 0;
    for value in values { total += value; }
    return total;
}
fn main(): i32 {
    let samples: [i32; 4] = [10, 20, 30, 40];
    let total = sum4(samples);
    $println(total);
    if total != 100 { return 1; }
    return 0;
}
```

Save it as `array-sum.vx` and run `vex run array-sum.vx`. Expected stdout is
`100` and exit status is 0. Passing a different length to `sum4` is a type
mismatch; length is not merely a runtime convention.

## Element access

Indices are zero-based. For an array of length `N`, valid element indices are
`0` through `N-1`; an empty array has no valid element index.

```vex
let! samples: [i32; 3] = [10, 20, 30];
samples[1] = 99;
$println(samples[1]); // 99
```

A mutable binding permits assignment to its elements when no conflicting
borrow is live. Use a checked container accessor or validate an external index
before indexing; do not rely on an out-of-bounds read yielding a sentinel.

## Iteration and transformation

For ordinary integer processing, an explicit loop makes the output shape and
ownership visible:

```vex [array-transform.vx]
fn main(): i32 {
    let input: [i32; 3] = [1, 2, 3];
    let! doubled: [i32; 3] = [0; 3];
    for index in 0..3 {
        doubled[index] = input[index] * 2;
    }
    $println(doubled[0]);
    $println(doubled[1]);
    $println(doubled[2]);
    return 0;
}
```

Expected stdout is `2`, `4`, and `6`, exit status 0. A transformation that can
change the number of elements should generally return a `Vec<T>` or an
explicit count, rather than pretending a fixed-size array has shrunk.

## Arithmetic and masks

The tensor/SIMD surface supports element-wise operations on compatible fixed
numeric arrays, scalar broadcast, and masks. The array lengths and element
types must fit the operation's requirements:

```vex
let left: [f32; 4] = [1.0, 2.0, 3.0, 4.0];
let right: [f32; 4] = [5.0, 6.0, 7.0, 8.0];
let sum = left + right; // element-wise: 6, 8, 10, 12
let shifted = left + 10.0;
```

These are numeric-operation fragments, not guarantees of a particular
instruction count. A source-level vector operation may become several
machine instructions, a loop, or scalar code depending on its shape, target
features, operation, and optimization settings. A compiler lowering threshold
such as 64 bytes is not a promise that every CPU has a register of that width.
See [SIMD](../simd/index.md) for operation and backend boundaries.

## Views and slices

A view borrows storage; it does not own the array or extend its lifetime. When
an API needs a `Span<T>`, use the supported view construction for that source
type and keep the owner alive for the entire use. Do not assume that all array,
vector, pack, and raw-pointer conversions have the same coercion rule.
The [Span guide](../memory/span-t.md) provides a complete example using the
explicit `Vec.asSpan()` interface and explains raw construction separately.

## Multidimensional arrays

Nested arrays encode each dimension:

```vex
let matrix: [[i32; 3]; 2] = [
    [1, 2, 3],
    [4, 5, 6],
];
let last = matrix[1][2]; // 6
```

The outer count is the number of rows; the inner count is the number of
elements in a row. Validate each index against its own dimension.

## Destructuring

A fixed array can be destructured in a `let` or `match` pattern:

```vex
let [red, green, blue] = [255 as u8, 128 as u8, 64 as u8];
let values = [10, 20, 30];
let total = match values { [a, b, c] => a + b + c, _ => 0 };
```

A trailing `..` ignores remaining elements. A trailing `...rest` binds them.
The rest must be the final array-pattern element: `[first, .., last]` is not
accepted. Do not assume that a rest binding is an independently owning copy;
its access and lifetime depend on the matched value and pattern context.
See [Advanced patterns](advanced-patterns.md).

## Copy and move semantics

An array is Copy when its elements satisfy the structural Copy rules. An array
of integers can therefore be assigned while the original remains usable.
An array containing owning boxes is not made Copy merely because its fixed
length is known. Assignment and destructuring must preserve element ownership;
use borrows when the original values need to remain in place.

## Choosing a collection

| Need | Usual choice |
| --- | --- |
| Fixed shape encoded in the type | `[T; N]` |
| Owned sequence with changing length | `Vec<T>` |
| Borrowed contiguous view | `Span<T>` |
| Heterogeneous group of values | A tuple or named struct |

## Related pages

- [Tuples](tuples.md)
- [Vectors](vec.md)
- [SIMD operations](../simd/simd-operations.md)
- [Pattern matching](pattern-matching.md)

<a id="type-signature"></a>
<a id="array-operations"></a>
<a id="length"></a>
<a id="slicing-and-span-conversion"></a>
<a id="array-to-tensor-auto-promotion"></a>
<a id="inline-simd-threshold"></a>
<a id="pattern-matching-on-arrays"></a>
<a id="copy-vs-non-copy-element-types"></a>
<a id="best-practices"></a>
