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 withN
copies ofx
. The type ofx
must beCopy
.
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:
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]
[T; N]: LengthAtMost32,
impl<const N: usize, T> AsRef<[T]> for [T; N] where
[T; N]: LengthAtMost32,
[src]
[T; N]: LengthAtMost32,
impl<const N: usize, T> Eq for [T; N] where
T: Eq,
[T; N]: LengthAtMost32,
[src]
T: Eq,
[T; N]: LengthAtMost32,
impl<const N: usize, T> Debug for [T; N] where
T: Debug,
[T; N]: LengthAtMost32,
[src]
T: Debug,
[T; N]: LengthAtMost32,
impl<'b, const N: usize, A, B> PartialEq<&'b [B]> for [A; N] where
A: PartialEq<B>,
[A; N]: LengthAtMost32,
[src]
A: PartialEq<B>,
[A; N]: LengthAtMost32,
impl<const N: usize, A, B> PartialEq<[B]> for [A; N] where
A: PartialEq<B>,
[A; N]: LengthAtMost32,
[src]
A: PartialEq<B>,
[A; N]: LengthAtMost32,
impl<'b, const N: usize, A, B> PartialEq<&'b mut [B]> for [A; N] where
A: PartialEq<B>,
[A; N]: LengthAtMost32,
[src]
A: PartialEq<B>,
[A; N]: LengthAtMost32,
fn eq(&self, other: &&'b mut [B]) -> bool
[src]
fn ne(&self, other: &&'b mut [B]) -> bool
[src]
impl<const N: usize, A, B> PartialEq<[B; N]> for [A; N] where
A: PartialEq<B>,
[A; N]: LengthAtMost32,
[B; N]: LengthAtMost32,
[src]
A: PartialEq<B>,
[A; N]: LengthAtMost32,
[B; N]: LengthAtMost32,
impl<const N: usize, T> PartialOrd<[T; N]> for [T; N] where
T: PartialOrd<T>,
[T; N]: LengthAtMost32,
[src]
T: PartialOrd<T>,
[T; N]: LengthAtMost32,
fn partial_cmp(&self, other: &[T; N]) -> Option<Ordering>
[src]
fn lt(&self, other: &[T; N]) -> bool
[src]
fn le(&self, other: &[T; N]) -> bool
[src]
fn ge(&self, other: &[T; N]) -> bool
[src]
fn gt(&self, other: &[T; N]) -> bool
[src]
impl<'a, const N: usize, T> IntoIterator for &'a mut [T; N] where
[T; N]: LengthAtMost32,
[src]
[T; N]: LengthAtMost32,
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?
ⓘImportant traits for IterMut<'a, T>fn into_iter(self) -> IterMut<'a, T>
[src]
impl<'a, const N: usize, T> IntoIterator for &'a [T; N] where
[T; N]: LengthAtMost32,
[src]
[T; N]: LengthAtMost32,
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?
ⓘImportant traits for Iter<'a, T>fn into_iter(self) -> Iter<'a, T>
[src]
impl<const N: usize, T> Hash for [T; N] where
T: Hash,
[T; N]: LengthAtMost32,
[src]
T: Hash,
[T; N]: LengthAtMost32,
fn hash<H>(&self, state: &mut H) where
H: Hasher,
[src]
H: Hasher,
fn hash_slice<H>(data: &[Self], state: &mut H) where
H: Hasher,
1.3.0[src]
H: Hasher,
impl<const N: usize, T> BorrowMut<[T]> for [T; N] where
[T; N]: LengthAtMost32,
1.4.0[src]
[T; N]: LengthAtMost32,
impl<const N: usize, T> AsMut<[T]> for [T; N] where
[T; N]: LengthAtMost32,
[src]
[T; N]: LengthAtMost32,
impl<const N: usize, T> Ord for [T; N] where
T: Ord,
[T; N]: LengthAtMost32,
[src]
T: Ord,
[T; N]: LengthAtMost32,
fn cmp(&self, other: &[T; N]) -> Ordering
[src]
fn max(self, other: Self) -> Self
1.21.0[src]
fn min(self, other: Self) -> Self
1.21.0[src]
fn clamp(self, min: Self, max: Self) -> Self
[src]
impl<'a, const N: usize, T> TryFrom<&'a [T]> for &'a [T; N] where
[T; N]: LengthAtMost32,
1.34.0[src]
[T; N]: LengthAtMost32,
type Error = TryFromSliceError
The type returned in the event of a conversion error.
fn try_from(slice: &[T]) -> Result<&[T; N], TryFromSliceError>
[src]
impl<'a, const N: usize, T> TryFrom<&'a mut [T]> for &'a mut [T; N] where
[T; N]: LengthAtMost32,
1.34.0[src]
[T; N]: LengthAtMost32,
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]
impl<'_, const N: usize, T> TryFrom<&'_ [T]> for [T; N] where
T: Copy,
[T; N]: LengthAtMost32,
1.34.0[src]
T: Copy,
[T; N]: LengthAtMost32,
type Error = TryFromSliceError
The type returned in the event of a conversion error.
fn try_from(slice: &[T]) -> Result<[T; N], TryFromSliceError>
[src]
impl<T> Default for [T; 32] where
T: Default,
1.4.0[src]
T: Default,
impl<T> Default for [T; 16] where
T: Default,
1.4.0[src]
T: Default,
impl<T> Default for [T; 22] where
T: Default,
1.4.0[src]
T: Default,
impl<T> Default for [T; 28] where
T: Default,
1.4.0[src]
T: Default,
impl<T> Default for [T; 23] where
T: Default,
1.4.0[src]
T: Default,
impl<T> Default for [T; 5] where
T: Default,
1.4.0[src]
T: Default,
impl<T> Default for [T; 9] where
T: Default,
1.4.0[src]
T: Default,
impl<T> Default for [T; 19] where
T: Default,
1.4.0[src]
T: Default,
impl<T> Default for [T; 30] where
T: Default,
1.4.0[src]
T: Default,
impl<T> Default for [T; 26] where
T: Default,
1.4.0[src]
T: Default,
impl<T> Default for [T; 10] where
T: Default,
1.4.0[src]
T: Default,
impl<T> Default for [T; 12] where
T: Default,
1.4.0[src]
T: Default,
impl<T> Default for [T; 27] where
T: Default,
1.4.0[src]
T: Default,
impl<T> Default for [T; 14] where
T: Default,
1.4.0[src]
T: Default,
impl<T> Default for [T; 25] where
T: Default,
1.4.0[src]
T: Default,
impl<T> Default for [T; 15] where
T: Default,
1.4.0[src]
T: Default,
impl<T> Default for [T; 1] where
T: Default,
1.4.0[src]
T: Default,
impl<T> Default for [T; 13] where
T: Default,
1.4.0[src]
T: Default,
impl<T> Default for [T; 8] where
T: Default,
1.4.0[src]
T: Default,
impl<T> Default for [T; 3] where
T: Default,
1.4.0[src]
T: Default,
impl<T> Default for [T; 31] where
T: Default,
1.4.0[src]
T: Default,
impl<T> Default for [T; 6] where
T: Default,
1.4.0[src]
T: Default,
impl<T> Default for [T; 17] where
T: Default,
1.4.0[src]
T: Default,
impl<T> Default for [T; 24] where
T: Default,
1.4.0[src]
T: Default,
impl<T> Default for [T; 11] where
T: Default,
1.4.0[src]
T: Default,
impl<T> Default for [T; 29] where
T: Default,
1.4.0[src]
T: Default,
impl<T> Default for [T; 4] where
T: Default,
1.4.0[src]
T: Default,
impl<T> Default for [T; 21] where
T: Default,
1.4.0[src]
T: Default,
impl<T> Default for [T; 2] where
T: Default,
1.4.0[src]
T: Default,
impl<T> Default for [T; 18] where
T: Default,
1.4.0[src]
T: Default,
impl<T> Default for [T; 7] where
T: Default,
1.4.0[src]
T: Default,
impl<T> Default for [T; 0]
1.4.0[src]
impl<T> Default for [T; 20] where
T: Default,
1.4.0[src]
T: Default,