A fixed-size array, denoted [T; N]
, for the element type, T
, and the
non-negative compile time constant size, N
.
Arrays values are created either with an explicit expression that lists
each element: [x, y, z]
or a repeat expression: [x; N]
. The repeat
expression requires that the element type is Copy
.
The type [T; N]
is Copy
if T: Copy
.
Arrays of sizes from 0 to 32 (inclusive) implement the following traits if
the element type allows it:
Clone
(only if T: Copy
)
Debug
IntoIterator
(implemented for &[T; N]
and &mut [T; N]
)
PartialEq
, PartialOrd
, Ord
, Eq
Hash
AsRef
, AsMut
Borrow
, BorrowMut
Default
Arrays coerce to slices ([T]
), so their methods can be called on
arrays.
Rust does not currently support generics over the size of an array type.
fn main() {
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);
}
}
let mut array: [i32; 3] = [0; 3];
array[1] = 1;
array[2] = 2;
assert_eq!([1, 2], &array[1..]);
for x in &array {
print!("{} ", x);
}
Trait Implementations
type Item = &'a T
type IntoIter = Iter<'a, T>
type Item = &'a mut T
type IntoIter = IterMut<'a, T>
impl<T> Eq for [T; 0] where T: Eq
type Item = &'a T
type IntoIter = Iter<'a, T>
type Item = &'a mut T
type IntoIter = IterMut<'a, T>
impl<T> Eq for [T; 1] where T: Eq
type Item = &'a T
type IntoIter = Iter<'a, T>
type Item = &'a mut T
type IntoIter = IterMut<'a, T>
impl<T> Eq for [T; 2] where T: Eq
type Item = &'a T
type IntoIter = Iter<'a, T>
type Item = &'a mut T
type IntoIter = IterMut<'a, T>
impl<T> Eq for [T; 3] where T: Eq
type Item = &'a T
type IntoIter = Iter<'a, T>
type Item = &'a mut T
type IntoIter = IterMut<'a, T>
impl<T> Eq for [T; 4] where T: Eq
type Item = &'a T
type IntoIter = Iter<'a, T>
type Item = &'a mut T
type IntoIter = IterMut<'a, T>
impl<T> Eq for [T; 5] where T: Eq
type Item = &'a T
type IntoIter = Iter<'a, T>
type Item = &'a mut T
type IntoIter = IterMut<'a, T>
impl<T> Eq for [T; 6] where T: Eq
type Item = &'a T
type IntoIter = Iter<'a, T>
type Item = &'a mut T
type IntoIter = IterMut<'a, T>
impl<T> Eq for [T; 7] where T: Eq
type Item = &'a T
type IntoIter = Iter<'a, T>
type Item = &'a mut T
type IntoIter = IterMut<'a, T>
impl<T> Eq for [T; 8] where T: Eq
type Item = &'a T
type IntoIter = Iter<'a, T>
type Item = &'a mut T
type IntoIter = IterMut<'a, T>
impl<T> Eq for [T; 9] where T: Eq
type Item = &'a T
type IntoIter = Iter<'a, T>
type Item = &'a mut T
type IntoIter = IterMut<'a, T>
impl<T> Eq for [T; 10] where T: Eq
type Item = &'a T
type IntoIter = Iter<'a, T>
type Item = &'a mut T
type IntoIter = IterMut<'a, T>
impl<T> Eq for [T; 11] where T: Eq
type Item = &'a T
type IntoIter = Iter<'a, T>
type Item = &'a mut T
type IntoIter = IterMut<'a, T>
impl<T> Eq for [T; 12] where T: Eq
type Item = &'a T
type IntoIter = Iter<'a, T>
type Item = &'a mut T
type IntoIter = IterMut<'a, T>
impl<T> Eq for [T; 13] where T: Eq
type Item = &'a T
type IntoIter = Iter<'a, T>
type Item = &'a mut T
type IntoIter = IterMut<'a, T>
impl<T> Eq for [T; 14] where T: Eq
type Item = &'a T
type IntoIter = Iter<'a, T>
type Item = &'a mut T
type IntoIter = IterMut<'a, T>
impl<T> Eq for [T; 15] where T: Eq
type Item = &'a T
type IntoIter = Iter<'a, T>
type Item = &'a mut T
type IntoIter = IterMut<'a, T>
impl<T> Eq for [T; 16] where T: Eq
type Item = &'a T
type IntoIter = Iter<'a, T>
type Item = &'a mut T
type IntoIter = IterMut<'a, T>
impl<T> Eq for [T; 17] where T: Eq
type Item = &'a T
type IntoIter = Iter<'a, T>
type Item = &'a mut T
type IntoIter = IterMut<'a, T>
impl<T> Eq for [T; 18] where T: Eq
type Item = &'a T
type IntoIter = Iter<'a, T>
type Item = &'a mut T
type IntoIter = IterMut<'a, T>
impl<T> Eq for [T; 19] where T: Eq
type Item = &'a T
type IntoIter = Iter<'a, T>
type Item = &'a mut T
type IntoIter = IterMut<'a, T>
impl<T> Eq for [T; 20] where T: Eq
type Item = &'a T
type IntoIter = Iter<'a, T>
type Item = &'a mut T
type IntoIter = IterMut<'a, T>
impl<T> Eq for [T; 21] where T: Eq
type Item = &'a T
type IntoIter = Iter<'a, T>
type Item = &'a mut T
type IntoIter = IterMut<'a, T>
impl<T> Eq for [T; 22] where T: Eq
type Item = &'a T
type IntoIter = Iter<'a, T>
type Item = &'a mut T
type IntoIter = IterMut<'a, T>
impl<T> Eq for [T; 23] where T: Eq
type Item = &'a T
type IntoIter = Iter<'a, T>
type Item = &'a mut T
type IntoIter = IterMut<'a, T>
impl<T> Eq for [T; 24] where T: Eq
type Item = &'a T
type IntoIter = Iter<'a, T>
type Item = &'a mut T
type IntoIter = IterMut<'a, T>
impl<T> Eq for [T; 25] where T: Eq
type Item = &'a T
type IntoIter = Iter<'a, T>
type Item = &'a mut T
type IntoIter = IterMut<'a, T>
impl<T> Eq for [T; 26] where T: Eq
type Item = &'a T
type IntoIter = Iter<'a, T>
type Item = &'a mut T
type IntoIter = IterMut<'a, T>
impl<T> Eq for [T; 27] where T: Eq
type Item = &'a T
type IntoIter = Iter<'a, T>
type Item = &'a mut T
type IntoIter = IterMut<'a, T>
impl<T> Eq for [T; 28] where T: Eq
type Item = &'a T
type IntoIter = Iter<'a, T>
type Item = &'a mut T
type IntoIter = IterMut<'a, T>
impl<T> Eq for [T; 29] where T: Eq
type Item = &'a T
type IntoIter = Iter<'a, T>
type Item = &'a mut T
type IntoIter = IterMut<'a, T>
impl<T> Eq for [T; 30] where T: Eq
type Item = &'a T
type IntoIter = Iter<'a, T>
type Item = &'a mut T
type IntoIter = IterMut<'a, T>
impl<T> Eq for [T; 31] where T: Eq
type Item = &'a T
type IntoIter = Iter<'a, T>
type Item = &'a mut T
type IntoIter = IterMut<'a, T>
impl<T> Eq for [T; 32] where T: Eq