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:

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

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 satisfiedRun

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<T> LengthAtMost32 for [T; 6][src]

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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?

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?

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

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

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

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

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.

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.

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.

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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