Rust Reference ยท Primitive Types
Rust arrays: the [T; N] type
Reference for Rust arrays: the [T; N] type, repeat expressions, indexing, iteration by value, map, from_fn, const generics, and converting slices to arrays.
An array is a fixed-size sequence of values of one type. Its type is written [T; N], where T is the element type and N is the length, which must be known at compile time. The length is part of the type, so [i32; 3] and [i32; 4] are different types. An array lives wherever you put it (usually the stack) and holds its elements inline, with no heap allocation and no length stored at run time.
You create arrays with a list, [1, 2, 3], or a repeat expression, [0; 8], which fills the array with eight copies of the value. For growable sequences use Vec<T>; to borrow part of an array use a slice.
let primes = [2, 3, 5, 7]; let zeros = [0u8; 16];
Try it
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.
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
let a: [T; N] = [e1, e2, e3]; let b = [value; N]; // value must be Copy, a const, or N must be 0 or 1 let c = [[0; COLS]; ROWS]; // nested arrays are rows of columns a[i] // indexing, i: usize a.len()
| Part | Description |
|---|---|
T |
The element type. All elements share it. |
N |
A usize constant expression, or a const generic parameter inside generic code. |
[value; N] |
Repeats value N times. It must be Copy or a const item; for other types use std::array::from_fn. |
Errors and edge cases: indexing with a constant that is out of range is rejected at compile time (the unconditional_panic lint); with a run-time index it panics with "index out of bounds". Use get for a checked Option<&T>.
Usage notes
- Arrays are values. Assigning or passing an array copies it when
T: Copy, and moves it otherwise. Large arrays get copied around, so pass&[T; N]or a slice when size matters. - Iteration by value (
for x in array) has worked since the 2021 edition and yieldsT, not&T. Use&arrayor.iter()to borrow instead. - Slice coercion:
&[T; N]converts to&[T]automatically, so most helper functions should take a slice. Methods likelen,iter,sortandcontainscome from the slice type. - Traits:
Debug,PartialEq,Eq,PartialOrd,Ord,HashandClonework for any length.Defaultis implemented for lengths up to 32 only. - Array methods:
map,each_ref,as_sliceandfrom_fnreturn or produce arrays;try_from(&[T])converts a slice of exactly the right length. - Pattern matching can destructure arrays by position, including a rest pattern
..(see slice patterns). - Stack overflow: a very large array such as
[0u8; 100_000_000]can overflow the stack. Usevec![0; n]orBox<[T]>for big buffers. - Const generics let you write functions that work for every length, as
first_twodoes above.
Related
[T] slices,Tuples,Vec<T>,Array methods,Slice patterns- Lessons: Arrays, Slices, Const generics