Skip to content
Tutorials Center
My learning

Topics

Rust Reference ยท Collections

Rust array methods

Reference for methods on Rust fixed-size arrays [T; N]: map, from_fn, each_ref, as_slice, iter, into_iter by value, try_from, zip-style helpers and limits.

An array [T; N] has a length known at compile time, and the standard library gives it a few methods of its own on top of everything it borrows from slices through automatic coercion. The array-specific ones are map (transform every element into a new array of the same length), std::array::from_fn (build an array from the index), each_ref and each_mut (an array of references), as_slice and as_mut_slice, and conversions such as TryFrom<&[T]> and TryFrom<Vec<T>>.

Since the 2021 edition arrays implement IntoIterator by value, so for x in [1, 2, 3] yields owned i32 values, not references.

Rust
let squares = [1, 2, 3].map(|n| n * n);          // [1, 4, 9]
let idx: [usize; 4] = std::array::from_fn(|i| i); // [0, 1, 2, 3]

Try it

Rust map and from_fn

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with io::stdin().read_line()), it gets the next line from this box.

Rust Conversions and each_ref

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with io::stdin().read_line()), it gets the next line from this box.

Syntax

Rust
impl<T, const N: usize> [T; N] {
    pub fn map<F, U>(self, f: F) -> [U; N] where F: FnMut(T) -> U;
    pub const fn as_slice(&self) -> &[T];
    pub fn as_mut_slice(&mut self) -> &mut [T];
    pub fn each_ref(&self) -> [&T; N];
    pub fn each_mut(&mut self) -> [&mut T; N];
}

pub fn std::array::from_fn<T, const N: usize, F>(cb: F) -> [T; N]
    where F: FnMut(usize) -> T;

impl<T: Copy, const N: usize> TryFrom<&[T]> for [T; N] { type Error = TryFromSliceError; }
impl<T, const N: usize> TryFrom<Vec<T>> for [T; N] { type Error = Vec<T>; }
Name Description
f (in map) Called once per element, in order, with the element moved in.
cb (in from_fn) Called with indices 0..N in order; returns the element.
N The length, a const generic; for from_fn it is inferred from the type you ask for.

Returns: map and from_fn return a new array of the same length N. try_from returns Err when the slice or Vec length is not exactly N.

Errors and panics: indexing past the end (arr[i]) panics; with a constant index the compiler rejects it. If the closure in map or from_fn panics, elements already produced are dropped correctly.

Usage notes

  • Fixed size is part of the type: [i32; 3] and [i32; 4] are different types. Use slices (&[i32]) in function parameters to accept any length.
  • Slice methods (len, iter, iter_mut, first, last, contains, sort, windows, chunks, concat, join, binary_search...) are available through coercion.
  • Copy and Clone: an array is Copy when its element type is, so let b = a; copies the whole array. Large arrays copy bytes, so prefer references.
  • No Default for big arrays: Default is implemented only for N up to 32. Use from_fn or [expr; N] instead.
  • Repeat syntax: [0; 100] needs Copy or a const element. For non-Copy values use from_fn(|_| String::new()).
  • Stack size: very large arrays live on the stack. Put them in a Box or Vec to avoid overflows.
  • Comparison and hashing work element-wise for any length; Debug is implemented for all N.
  • map is eager and builds the new array immediately, unlike iterator adapters.