# 1.0.0[−]Primitive Type array

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 list with each element, i.e., `[x, y, z]`.
- A repeat expression `[x; N]`, which produces an array with `N` copies of `x`. The type of `x` must be `Copy`.

Arrays of sizes from 0 to 32 (inclusive) implement the following traits if the element type allows it:

- `Debug`
- `IntoIterator` (implemented for `&[T; N]` and `&mut [T; N]`)
- `PartialEq`, `PartialOrd`, `Eq`, `Ord`
- `Hash`
- `AsRef`, `AsMut`
- `Borrow`, `BorrowMut`
- `Default`

This limitation on the size `N` exists because Rust does not yet support code that is generic over the size of an array type. `[Foo; 3]` and `[Bar; 3]` are instances of same generic type `[T; 3]`, but `[Foo; 3]` and `[Foo; 5]` are entirely different types. As a stopgap, trait implementations are statically generated up to size 32.

Arrays of any size are `Copy` if the element type is `Copy` and `Clone` if the element type is `Clone`. This works because `Copy` and `Clone` traits are specially known to the compiler.

Arrays coerce to slices (`[T]`), so a slice method may be called on an array. Indeed, this provides most of the API for working with arrays. Slices have a dynamic size and do not coerce to arrays.

You can move elements out of an array with a slice pattern. If you want one element, see `mem::replace`.

# Examples

    let mut array: [i32; 3] = [0; 3];

    array[1] = 1;
    array[2] = 2;

    assert_eq!([1, 2], &array[1..]);

    // This loop prints: 0 1 2
    for x in &array {
        print!("{} ", x);
    }

Run

An array itself is not iterable:

ⓘThis example deliberately fails to compile

    let array: [i32; 3] = [0; 3];

    for x in array { }
    // error: the trait bound `[i32; 3]: std::iter::Iterator` is not satisfied

Run

The solution is to coerce the array to a slice by calling a slice method:

    for x in array.iter() { }

Run

If the array has 32 or fewer elements (see above), you can also use the array reference's `IntoIterator` implementation:

    for x in &array { }

Run

You can use a slice pattern to move elements out of an array:

    fn move_away(_: String) { /* Do interesting things. */ }

    let [john, roa] = ["John".to_string(), "Roa".to_string()];
    move_away(john);
    move_away(roa);

Run

## Trait Implementations

### `impl<const N: usize, T> AsMut<[T]> for [T; N] where
[T; N]: LengthAtMost32,` [src]

#### `fn as_mut(&mut self) -> &mut [T]`[src]

Performs the conversion.

### `impl<const N: usize, T> AsRef<[T]> for [T; N] where
[T; N]: LengthAtMost32,` [src]

#### `fn as_ref(&self) -> &[T]`[src]

Performs the conversion.

### `impl<const N: usize, T> Borrow<[T]> for [T; N] where
[T; N]: LengthAtMost32,` 1.4.0[src]

#### `fn borrow(&self) -> &[T]`[src]

Immutably borrows from an owned value. Read more

### `impl<const N: usize, T> BorrowMut<[T]> for [T; N] where
[T; N]: LengthAtMost32,` 1.4.0[src]

#### `fn borrow_mut(&mut self) -> &mut [T]`[src]

Mutably borrows from an owned value. Read more

### `impl<const N: usize, T> Debug for [T; N] where
T: Debug,
[T; N]: LengthAtMost32,` [src]

#### `fn fmt(&self, f: &mut Formatter) -> Result<(), Error>`[src]

Formats the value using the given formatter. Read more

### `impl<T> Default for [T; 2] where
T: Default,` 1.4.0[src]

#### `fn default() -> [T; 2]`[src]

Returns the "default value" for a type. Read more

### `impl<T> Default for [T; 32] where
T: Default,` 1.4.0[src]

#### `fn default() -> [T; 32]`[src]

Returns the "default value" for a type. Read more

### `impl<T> Default for [T; 25] where
T: Default,` 1.4.0[src]

#### `fn default() -> [T; 25]`[src]

Returns the "default value" for a type. Read more

### `impl<T> Default for [T; 24] where
T: Default,` 1.4.0[src]

#### `fn default() -> [T; 24]`[src]

Returns the "default value" for a type. Read more

### `impl<T> Default for [T; 6] where
T: Default,` 1.4.0[src]

#### `fn default() -> [T; 6]`[src]

Returns the "default value" for a type. Read more

### `impl<T> Default for [T; 9] where
T: Default,` 1.4.0[src]

#### `fn default() -> [T; 9]`[src]

Returns the "default value" for a type. Read more

### `impl<T> Default for [T; 18] where
T: Default,` 1.4.0[src]

#### `fn default() -> [T; 18]`[src]

Returns the "default value" for a type. Read more

### `impl<T> Default for [T; 17] where
T: Default,` 1.4.0[src]

#### `fn default() -> [T; 17]`[src]

Returns the "default value" for a type. Read more

### `impl<T> Default for [T; 22] where
T: Default,` 1.4.0[src]

#### `fn default() -> [T; 22]`[src]

Returns the "default value" for a type. Read more

### `impl<T> Default for [T; 4] where
T: Default,` 1.4.0[src]

#### `fn default() -> [T; 4]`[src]

Returns the "default value" for a type. Read more

### `impl<T> Default for [T; 28] where
T: Default,` 1.4.0[src]

#### `fn default() -> [T; 28]`[src]

Returns the "default value" for a type. Read more

### `impl<T> Default for [T; 13] where
T: Default,` 1.4.0[src]

#### `fn default() -> [T; 13]`[src]

Returns the "default value" for a type. Read more

### `impl<T> Default for [T; 1] where
T: Default,` 1.4.0[src]

#### `fn default() -> [T; 1]`[src]

Returns the "default value" for a type. Read more

### `impl<T> Default for [T; 10] where
T: Default,` 1.4.0[src]

#### `fn default() -> [T; 10]`[src]

Returns the "default value" for a type. Read more

### `impl<T> Default for [T; 7] where
T: Default,` 1.4.0[src]

#### `fn default() -> [T; 7]`[src]

Returns the "default value" for a type. Read more

### `impl<T> Default for [T; 3] where
T: Default,` 1.4.0[src]

#### `fn default() -> [T; 3]`[src]

Returns the "default value" for a type. Read more

### `impl<T> Default for [T; 19] where
T: Default,` 1.4.0[src]

#### `fn default() -> [T; 19]`[src]

Returns the "default value" for a type. Read more

### `impl<T> Default for [T; 12] where
T: Default,` 1.4.0[src]

#### `fn default() -> [T; 12]`[src]

Returns the "default value" for a type. Read more

### `impl<T> Default for [T; 8] where
T: Default,` 1.4.0[src]

#### `fn default() -> [T; 8]`[src]

Returns the "default value" for a type. Read more

### `impl<T> Default for [T; 5] where
T: Default,` 1.4.0[src]

#### `fn default() -> [T; 5]`[src]

Returns the "default value" for a type. Read more

### `impl<T> Default for [T; 21] where
T: Default,` 1.4.0[src]

#### `fn default() -> [T; 21]`[src]

Returns the "default value" for a type. Read more

### `impl<T> Default for [T; 30] where
T: Default,` 1.4.0[src]

#### `fn default() -> [T; 30]`[src]

Returns the "default value" for a type. Read more

### `impl<T> Default for [T; 20] where
T: Default,` 1.4.0[src]

#### `fn default() -> [T; 20]`[src]

Returns the "default value" for a type. Read more

### `impl<T> Default for [T; 23] where
T: Default,` 1.4.0[src]

#### `fn default() -> [T; 23]`[src]

Returns the "default value" for a type. Read more

### `impl<T> Default for [T; 26] where
T: Default,` 1.4.0[src]

#### `fn default() -> [T; 26]`[src]

Returns the "default value" for a type. Read more

### `impl<T> Default for [T; 29] where
T: Default,` 1.4.0[src]

#### `fn default() -> [T; 29]`[src]

Returns the "default value" for a type. Read more

### `impl<T> Default for [T; 31] where
T: Default,` 1.4.0[src]

#### `fn default() -> [T; 31]`[src]

Returns the "default value" for a type. Read more

### `impl<T> Default for [T; 14] where
T: Default,` 1.4.0[src]

#### `fn default() -> [T; 14]`[src]

Returns the "default value" for a type. Read more

### `impl<T> Default for [T; 27] where
T: Default,` 1.4.0[src]

#### `fn default() -> [T; 27]`[src]

Returns the "default value" for a type. Read more

### `impl<T> Default for [T; 15] where
T: Default,` 1.4.0[src]

#### `fn default() -> [T; 15]`[src]

Returns the "default value" for a type. Read more

### `impl<T> Default for [T; 11] where
T: Default,` 1.4.0[src]

#### `fn default() -> [T; 11]`[src]

Returns the "default value" for a type. Read more

### `impl<T> Default for [T; 0]`1.4.0[src]

#### `fn default() -> [T; 0]`[src]

Returns the "default value" for a type. Read more

### `impl<T> Default for [T; 16] where
T: Default,` 1.4.0[src]

#### `fn default() -> [T; 16]`[src]

Returns the "default value" for a type. Read more

### `impl<const N: usize, T> Eq for [T; N] where
T: Eq,
[T; N]: LengthAtMost32,` [src]

### `impl<const N: usize, T> Hash for [T; N] where
T: Hash,
[T; N]: LengthAtMost32,` [src]

#### `fn hash<H>(&self, state: &mut H) where
H: Hasher,` [src]

Feeds this value into the given [`Hasher`]. Read more

#### `fn hash_slice<H>(data: &[Self], state: &mut H) where
H: Hasher,` 1.3.0[src]

Feeds a slice of this type into the given [`Hasher`]. Read more

### `impl<'a, const N: usize, T> IntoIterator for &'a mut [T; N] where
[T; N]: LengthAtMost32,` [src]

#### `type Item = &'a mut T`

The type of the elements being iterated over.

#### `type IntoIter = IterMut<'a, T>`

Which kind of iterator are we turning this into?

#### `fn into_iter(self) -> IterMut<'a, T>`[src]

Creates an iterator from a value. Read more

### `impl<'a, const N: usize, T> IntoIterator for &'a [T; N] where
[T; N]: LengthAtMost32,` [src]

#### `type Item = &'a T`

The type of the elements being iterated over.

#### `type IntoIter = Iter<'a, T>`

Which kind of iterator are we turning this into?

#### `fn into_iter(self) -> Iter<'a, T>`[src]

Creates an iterator from a value. Read more

### `impl<T> LengthAtMost32 for [T; 14]`[src]

### `impl<T> LengthAtMost32 for [T; 8]`[src]

### `impl<T> LengthAtMost32 for [T; 13]`[src]

### `impl<T> LengthAtMost32 for [T; 28]`[src]

### `impl<T> LengthAtMost32 for [T; 1]`[src]

### `impl<T> LengthAtMost32 for [T; 16]`[src]

### `impl<T> LengthAtMost32 for [T; 29]`[src]

### `impl<T> LengthAtMost32 for [T; 10]`[src]

### `impl<T> LengthAtMost32 for [T; 19]`[src]

### `impl<T> LengthAtMost32 for [T; 7]`[src]

### `impl<T> LengthAtMost32 for [T; 15]`[src]

### `impl<T> LengthAtMost32 for [T; 25]`[src]

### `impl<T> LengthAtMost32 for [T; 6]`[src]

### `impl<T> LengthAtMost32 for [T; 20]`[src]

### `impl<T> LengthAtMost32 for [T; 11]`[src]

### `impl<T> LengthAtMost32 for [T; 27]`[src]

### `impl<T> LengthAtMost32 for [T; 17]`[src]

### `impl<T> LengthAtMost32 for [T; 5]`[src]

### `impl<T> LengthAtMost32 for [T; 23]`[src]

### `impl<T> LengthAtMost32 for [T; 9]`[src]

### `impl<T> LengthAtMost32 for [T; 24]`[src]

### `impl<T> LengthAtMost32 for [T; 21]`[src]

### `impl<T> LengthAtMost32 for [T; 30]`[src]

### `impl<T> LengthAtMost32 for [T; 32]`[src]

### `impl<T> LengthAtMost32 for [T; 26]`[src]

### `impl<T> LengthAtMost32 for [T; 12]`[src]

### `impl<T> LengthAtMost32 for [T; 31]`[src]

### `impl<T> LengthAtMost32 for [T; 0]`[src]

### `impl<T> LengthAtMost32 for [T; 22]`[src]

### `impl<T> LengthAtMost32 for [T; 3]`[src]

### `impl<T> LengthAtMost32 for [T; 18]`[src]

### `impl<T> LengthAtMost32 for [T; 4]`[src]

### `impl<T> LengthAtMost32 for [T; 2]`[src]

### `impl<const N: usize, T> Ord for [T; N] where
T: Ord,
[T; N]: LengthAtMost32,` [src]

#### `fn cmp(&self, other: &[T; N]) -> Ordering`[src]

This method returns an [`Ordering`] between `self` and `other`. Read more

#### `#[must_use] fn max(self, other: Self) -> Self` 1.21.0[src]

Compares and returns the maximum of two values. Read more

#### `#[must_use] fn min(self, other: Self) -> Self` 1.21.0[src]

Compares and returns the minimum of two values. Read more

#### `#[must_use] fn clamp(self, min: Self, max: Self) -> Self`[src]

🔬 This is a nightly-only experimental API. (`clamp` #44095)

Restrict a value to a certain interval. Read more

### `impl<'b, const N: usize, A, B> PartialEq<&'b [B]> for [A; N] where
A: PartialEq<B>,
[A; N]: LengthAtMost32,` [src]

#### `fn eq(&self, other: &&'b [B]) -> bool`[src]

This method tests for `self` and `other` values to be equal, and is used by `==`. Read more

#### `fn ne(&self, other: &&'b [B]) -> bool`[src]

This method tests for `!=`.

### `impl<'b, const N: usize, A, B> PartialEq<&'b mut [B]> for [A; N] where
A: PartialEq<B>,
[A; N]: LengthAtMost32,` [src]

#### `fn eq(&self, other: &&'b mut [B]) -> bool`[src]

This method tests for `self` and `other` values to be equal, and is used by `==`. Read more

#### `fn ne(&self, other: &&'b mut [B]) -> bool`[src]

This method tests for `!=`.

### `impl<const N: usize, A, B> PartialEq<[B; N]> for [A; N] where
A: PartialEq<B>,
[A; N]: LengthAtMost32,
[B; N]: LengthAtMost32,` [src]

#### `fn eq(&self, other: &[B; N]) -> bool`[src]

This method tests for `self` and `other` values to be equal, and is used by `==`. Read more

#### `fn ne(&self, other: &[B; N]) -> bool`[src]

This method tests for `!=`.

### `impl<const N: usize, A, B> PartialEq<[B]> for [A; N] where
A: PartialEq<B>,
[A; N]: LengthAtMost32,` [src]

#### `fn eq(&self, other: &[B]) -> bool`[src]

This method tests for `self` and `other` values to be equal, and is used by `==`. Read more

#### `fn ne(&self, other: &[B]) -> bool`[src]

This method tests for `!=`.

### `impl<const N: usize, T> PartialOrd<[T; N]> for [T; N] where
T: PartialOrd<T>,
[T; N]: LengthAtMost32,` [src]

#### `fn partial_cmp(&self, other: &[T; N]) -> Option<Ordering>`[src]

This method returns an ordering between `self` and `other` values if one exists. Read more

#### `fn lt(&self, other: &[T; N]) -> bool`[src]

This method tests less than (for `self` and `other`) and is used by the `<` operator. Read more

#### `fn le(&self, other: &[T; N]) -> bool`[src]

This method tests less than or equal to (for `self` and `other`) and is used by the `<=` operator. Read more

#### `fn ge(&self, other: &[T; N]) -> bool`[src]

This method tests greater than or equal to (for `self` and `other`) and is used by the `>=` operator. Read more

#### `fn gt(&self, other: &[T; N]) -> bool`[src]

This method tests greater than (for `self` and `other`) and is used by the `>` operator. Read more

### `impl<'_, const N: usize, T> TryFrom<&'_ [T]> for [T; N] where
T: Copy,
[T; N]: LengthAtMost32,` 1.34.0[src]

#### `type Error = TryFromSliceError`

The type returned in the event of a conversion error.

#### `fn try_from(slice: &[T]) -> Result<[T; N], TryFromSliceError>`[src]

Performs the conversion.

### `impl<'a, const N: usize, T> TryFrom<&'a [T]> for &'a [T; N] where
[T; N]: LengthAtMost32,` 1.34.0[src]

#### `type Error = TryFromSliceError`

The type returned in the event of a conversion error.

#### `fn try_from(slice: &[T]) -> Result<&[T; N], TryFromSliceError>`[src]

Performs the conversion.

### `impl<'a, const N: usize, T> TryFrom<&'a mut [T]> for &'a mut [T; N] where
[T; N]: LengthAtMost32,` 1.34.0[src]

#### `type Error = TryFromSliceError`

The type returned in the event of a conversion error.

#### `fn try_from(slice: &mut [T]) -> Result<&mut [T; N], TryFromSliceError>`[src]

Performs the conversion.