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:
[x, y, z]
.[x; N]
, which produces an array with N
copies of x
. The type of x
must be Copy
.Arrays of any size 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
Arrays of sizes from 0 to 32 (inclusive) implement Default
trait if the element type allows it. 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
.
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); }
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 satisfied
The solution is to coerce the array to a slice by calling a slice method:
for x in array.iter() { }
You can also use the array reference's IntoIterator
implementation:
for x in &array { }
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);
impl<T, const N: usize> [T; N]
[src]
pub fn map<F, U>(self, f: F) -> [U; N] where
F: FnMut(T) -> U,
[src]
Returns an array of the same size as self
, with function f
applied to each element in order.
#![feature(array_map)] let x = [1, 2, 3]; let y = x.map(|v| v + 1); assert_eq!(y, [2, 3, 4]); let x = [1, 2, 3]; let mut temp = 0; let y = x.map(|v| { temp += 1; v * temp }); assert_eq!(y, [1, 4, 9]); let x = ["Ferris", "Bueller's", "Day", "Off"]; let y = x.map(|v| v.len()); assert_eq!(y, [6, 9, 3, 3]);
impl<T, const N: usize> AsMut<[T]> for [T; N]
[src]
fn as_mut(&mut self) -> &mut [T]ⓘNotable traits for &'_ [u8]
impl<'_> Read for &'_ [u8]
impl<'_> Write for &'_ mut [u8]
[src]
impl<T, const N: usize> AsRef<[T]> for [T; N]
[src]
fn as_ref(&self) -> &[T]ⓘNotable traits for &'_ [u8]
impl<'_> Read for &'_ [u8]
impl<'_> Write for &'_ mut [u8]
[src]
impl<T, const N: usize> Borrow<[T]> for [T; N]
[src]1.4.0
fn borrow(&self) -> &[T]ⓘNotable traits for &'_ [u8]
impl<'_> Read for &'_ [u8]
impl<'_> Write for &'_ mut [u8]
[src]
impl<T, const N: usize> BorrowMut<[T]> for [T; N]
[src]1.4.0
fn borrow_mut(&mut self) -> &mut [T]ⓘNotable traits for &'_ [u8]
impl<'_> Read for &'_ [u8]
impl<'_> Write for &'_ mut [u8]
[src]
impl<T, const N: usize> Debug for [T; N] where
T: Debug,
[src]
impl<T> Default for [T; 21] where
T: Default,
[src]1.4.0
impl<T> Default for [T; 20] where
T: Default,
[src]1.4.0
impl<T> Default for [T; 32] where
T: Default,
[src]1.4.0
impl<T> Default for [T; 31] where
T: Default,
[src]1.4.0
impl<T> Default for [T; 13] where
T: Default,
[src]1.4.0
impl<T> Default for [T; 30] where
T: Default,
[src]1.4.0
impl<T> Default for [T; 22] where
T: Default,
[src]1.4.0
impl<T> Default for [T; 9] where
T: Default,
[src]1.4.0
impl<T> Default for [T; 6] where
T: Default,
[src]1.4.0
impl<T> Default for [T; 8] where
T: Default,
[src]1.4.0
impl<T> Default for [T; 1] where
T: Default,
[src]1.4.0
impl<T> Default for [T; 2] where
T: Default,
[src]1.4.0
impl<T> Default for [T; 17] where
T: Default,
[src]1.4.0
impl<T> Default for [T; 7] where
T: Default,
[src]1.4.0
impl<T> Default for [T; 10] where
T: Default,
[src]1.4.0
impl<T> Default for [T; 12] where
T: Default,
[src]1.4.0
impl<T> Default for [T; 5] where
T: Default,
[src]1.4.0
impl<T> Default for [T; 3] where
T: Default,
[src]1.4.0
impl<T> Default for [T; 11] where
T: Default,
[src]1.4.0
impl<T> Default for [T; 4] where
T: Default,
[src]1.4.0
impl<T> Default for [T; 23] where
T: Default,
[src]1.4.0
impl<T> Default for [T; 18] where
T: Default,
[src]1.4.0
impl<T> Default for [T; 25] where
T: Default,
[src]1.4.0
impl<T> Default for [T; 29] where
T: Default,
[src]1.4.0
impl<T> Default for [T; 28] where
T: Default,
[src]1.4.0
impl<T> Default for [T; 27] where
T: Default,
[src]1.4.0
impl<T> Default for [T; 24] where
T: Default,
[src]1.4.0
impl<T> Default for [T; 26] where
T: Default,
[src]1.4.0
impl<T> Default for [T; 0]
[src]1.4.0
impl<T> Default for [T; 14] where
T: Default,
[src]1.4.0
impl<T> Default for [T; 15] where
T: Default,
[src]1.4.0
impl<T> Default for [T; 16] where
T: Default,
[src]1.4.0
impl<T> Default for [T; 19] where
T: Default,
[src]1.4.0
impl<T, const N: usize> Eq for [T; N] where
T: Eq,
[src]
impl<T, const N: usize> Hash for [T; N] where
T: Hash,
[src]
fn hash<H>(&self, state: &mut H) where
H: Hasher,
[src]
fn hash_slice<H>(data: &[Self], state: &mut H) where
H: Hasher,
[src]1.3.0
impl<'a, T, const N: usize> IntoIterator for &'a [T; N]
[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>ⓘNotable traits for Iter<'a, T>
impl<'a, T> Iterator for Iter<'a, T>
type Item = &'a T;
[src]
impl<'a, T, const N: usize> IntoIterator for &'a mut [T; N]
[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>ⓘNotable traits for IterMut<'a, T>
impl<'a, T> Iterator for IterMut<'a, T>
type Item = &'a mut T;
[src]
impl<T, const N: usize> Ord for [T; N] where
T: Ord,
[src]
Implements comparison of arrays lexicographically.
fn cmp(&self, other: &[T; N]) -> Ordering
[src]
fn max(self, other: Self) -> Self
[src]1.21.0
fn min(self, other: Self) -> Self
[src]1.21.0
fn clamp(self, min: Self, max: Self) -> Self
[src]
impl<'b, A, B, const N: usize> PartialEq<&'b [B]> for [A; N] where
A: PartialEq<B>,
[src]
impl<'b, A, B, const N: usize> PartialEq<&'b mut [B]> for [A; N] where
A: PartialEq<B>,
[src]
fn eq(&self, other: &&'b mut [B]) -> bool
[src]
fn ne(&self, other: &&'b mut [B]) -> bool
[src]
impl<A, B, const N: usize> PartialEq<[B; N]> for [A; N] where
A: PartialEq<B>,
[src]
impl<A, B, const N: usize> PartialEq<[B]> for [A; N] where
A: PartialEq<B>,
[src]
impl<T, const N: usize> PartialOrd<[T; N]> for [T; N] where
T: PartialOrd<T>,
[src]
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<'_, T, const N: usize> TryFrom<&'_ [T]> for [T; N] where
T: Copy,
[src]1.34.0
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, T, const N: usize> TryFrom<&'a [T]> for &'a [T; N]
[src]1.34.0
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, T, const N: usize> TryFrom<&'a mut [T]> for &'a mut [T; N]
[src]1.34.0
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<T, const N: usize> RefUnwindSafe for [T; N] where
T: RefUnwindSafe,
impl<T, const N: usize> Send for [T; N] where
T: Send,
impl<T, const N: usize> Sync for [T; N] where
T: Sync,
impl<T, const N: usize> Unpin for [T; N] where
T: Unpin,
impl<T, const N: usize> UnwindSafe for [T; N] where
T: UnwindSafe,
impl<T> Any for T where
T: 'static + ?Sized,
[src]
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
fn borrow(&self) -> &TⓘNotable traits for &'_ mut F
impl<'_, F> Future for &'_ mut F where
F: Unpin + Future + ?Sized,
type Output = <F as Future>::Output;
impl<'_, I> Iterator for &'_ mut I where
I: Iterator + ?Sized,
type Item = <I as Iterator>::Item;
impl<R: Read + ?Sized, '_> Read for &'_ mut R
impl<W: Write + ?Sized, '_> Write for &'_ mut W
[src]
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
fn borrow_mut(&mut self) -> &mut TⓘNotable traits for &'_ mut F
impl<'_, F> Future for &'_ mut F where
F: Unpin + Future + ?Sized,
type Output = <F as Future>::Output;
impl<'_, I> Iterator for &'_ mut I where
I: Iterator + ?Sized,
type Item = <I as Iterator>::Item;
impl<R: Read + ?Sized, '_> Read for &'_ mut R
impl<W: Write + ?Sized, '_> Write for &'_ mut W
[src]
impl<T, A> FixedSizeArray<T> for A where
A: Unsize<[T]>,
[src]
fn as_slice(&self) -> &[T]ⓘNotable traits for &'_ [u8]
impl<'_> Read for &'_ [u8]
impl<'_> Write for &'_ mut [u8]
[src]
fn as_mut_slice(&mut self) -> &mut [T]ⓘNotable traits for &'_ [u8]
impl<'_> Read for &'_ [u8]
impl<'_> Write for &'_ mut [u8]
[src]
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
impl<T> ToOwned for T where
T: Clone,
[src]
type Owned = T
The resulting type after obtaining ownership.
fn to_owned(&self) -> T
[src]
fn clone_into(&self, target: &mut T)
[src]
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
type Error = Infallible
The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
© 2010 The Rust Project Developers
Licensed under the Apache License, Version 2.0 or the MIT license, at your option.
https://doc.rust-lang.org/std/primitive.array.html