> Aztec.nr API reference, v6.0.0 (mainnet and testnet). Index: https://docs.aztec.network/aztec-nr-api/v6.0.0/llms.txt

# Primitive type array

Module: `std`

A fixed-size array, denoted `[T; N]`, for the element type, `T`, and the non-negative compile-time constant size, `N`.

There are two syntactic forms for creating an array:

- A vector with each element, i.e., `[x, y, z]`.
- A repeat expression `[expr; N]` where `N` is how many times to repeat `expr` in the array.

## Implementations

```rust
impl<T, let N: u32> [T; N]
```

```rust
pub fn len(self) -> u32
```

Returns the length of this array.

```rust
fn len(self) -> Field
```

example

```rust
fn main() {
    let array = [42, 42];
    assert(array.len() == 2);
}
```

```rust
pub fn as_vector(self) -> [T]
```

Returns this array as a vector.

```rust
let array = [1, 2];
let vector = array.as_vector();
assert_eq(vector, [1, 2].as_vector());
```

```rust
pub fn map<U, Env>(&self, f: fn[Env](T) -> U) -> [U; N]
```

Applies a function to each element of this array, returning a new array containing the mapped elements.

Example:

```rust
let a = [1, 2, 3];
let b = a.map(|a| a * 2);
assert_eq(b, [2, 4, 6]);
```

```rust
pub fn mapi<U, Env>(&self, f: fn[Env](u32, T) -> U) -> [U; N]
```

Applies a function to each element of this array along with its index, returning a new array containing the mapped elements.

Example:

```rust
let a = [1, 2, 3];
let b = a.mapi(|i, a| i + a * 2);
assert_eq(b, [2, 5, 8]);
```

```rust
pub fn for_each<Env>(&self, f: fn[Env](T))
```

Applies a function to each element of this array.

Example:

```rust
let a = [1, 2, 3];
let mut b = [0; 3];
let mut i = 0;
a.for_each(|x| {
    b[i] = x;
    i += 1;
});
assert_eq(a, b);
```

```rust
pub fn for_eachi<Env>(&self, f: fn[Env](u32, T))
```

Applies a function to each element of this array along with its index.

Example:

```rust
let a = [1, 2, 3];
let mut b = [0; 3];
a.for_eachi(|i, x| {
    b[i] = x;
});
assert_eq(a, b);
```

```rust
pub fn fold<U, Env>(&self, accumulator: U, f: fn[Env](U, T) -> U) -> U
```

Applies a function to each element of the array, returning the final accumulated value. The first parameter is the initial value.

This is a left fold, so the given function will be applied to the accumulator and first element of the array, then the second, and so on. For a given call the expected result would be equivalent to:

```rust
let a1 = [1];
let a2 = [1, 2];
let a3 = [1, 2, 3];

let f = |a, b| a - b;
a1.fold(10, f); //=> f(10, 1)
a2.fold(10, f); //=> f(f(10, 1), 2)
a3.fold(10, f); //=> f(f(f(10, 1), 2), 3)

assert_eq(a3.fold(10, f), 10 - 1 - 2 - 3);
```

```rust
pub fn reduce<Env>(&self, f: fn[Env](T, T) -> T) -> T
```

Same as fold, but uses the first element as the starting element.

Requires the input array to be non-empty.

Example:

```rust
fn main() {
    let arr = [1, 2, 3, 4];
    let reduced = arr.reduce(|a, b| a + b);
    assert(reduced == 10);
}
```

```rust
pub fn all<Env>(&self, predicate: fn[Env](T) -> bool) -> bool
```

Returns true if all the elements in this array satisfy the given predicate.

Example:

```rust
fn main() {
    let arr = [2, 2, 2, 2, 2];
    let all = arr.all(|a| a == 2);
    assert(all);
}
```

```rust
pub fn any<Env>(&self, predicate: fn[Env](T) -> bool) -> bool
```

Returns true if any of the elements in this array satisfy the given predicate.

Example:

```rust
fn main() {
    let arr = [2, 2, 2, 2, 5];
    let any = arr.any(|a| a == 5);
    assert(any);
}
```

```rust
pub fn concat<let M: u32>(&self, array2: [T; M]) -> [T; N + M]
```

Concatenates this array with another array.

Example:

```rust
fn main() {
    let arr1 = [1, 2, 3, 4];
    let arr2 = [6, 7, 8, 9, 10, 11];
    let concatenated_arr = arr1.concat(arr2);
    assert(concatenated_arr == [1, 2, 3, 4, 6, 7, 8, 9, 10, 11]);
}
```

```rust
impl<T, let N: u32> [T; N]
where
    T: Ord,
    T: Eq
```

```rust
pub fn sort(&self) -> Self
```

Returns a new sorted array. The original array remains untouched. Notice that this function will only work for arrays of fields or integers, not for any arbitrary type. This is because the sorting logic it uses internally is optimized specifically for these values. If you need a sort function to sort any type, you should use the [`Self::sort_via`](/aztec-nr-api/v6.0.0/std/primitive.array.md#sort_via) function.

Example:

```rust
fn main() {
    let arr = [42, 32];
    let sorted = arr.sort();
    assert(sorted == [32, 42]);
}
```

```rust
impl<T, let N: u32> [T; N]
where
    T: Eq
```

```rust
pub fn sort_via<Env>(&self, ordering: fn[Env](T, T) -> bool) -> Self
```

Returns a new sorted array by sorting it with a custom comparison function. The original array remains untouched. The ordering function must return true if the first argument should be sorted to be before the second argument or is equal to the second argument.

Using this method with an operator like `<` that does not return `true` for equal values will result in an assertion failure for arrays with equal elements.

Example:

```rust
fn main() {
    let arr = [42, 32]
    let sorted_ascending = arr.sort_via(|a, b| a <= b);
    assert(sorted_ascending == [32, 42]); // verifies

    let sorted_descending = arr.sort_via(|a, b| a >= b);
    assert(sorted_descending == [32, 42]); // does not verify
}
```

```rust
impl<let N: u32> [u8; N]
```

```rust
pub fn as_str_unchecked(self) -> str<N>
```

Converts a byte array of type `[u8; N]` to a string. Note that this performs no UTF-8 validation - the given array is interpreted as-is as a string.

Example:

```rust
fn main() {
    let hi = [104, 105].as_str_unchecked();
    assert_eq(hi, "hi");
}
```
