array
Module for Array
and other continuous same type collections. A contiguous collection of elements of the same type in memory, written Array<T>
. Arrays have O(1) indexing, O(1) push and O(1) pop (from the front). Arrays can only be mutated by appending to the end or popping from the front. # Examples You can explicitly create an Array
with ArrayTrait::new
:
let arr: Array<usize> = ArrayTrait::new();
...or by using the array!
macro:
let arr: Array<usize> = array![];
let arr: Array<usize> = array![1, 2, 3, 4, 5];
You can append
values onto the end of an array:
let mut arr = array![1, 2];
arr.append(3);
Popping values from the front works like this:
let mut arr = array![1, 2];
let one = arr.pop_front(); // Returns Option::Some(1)
Arrays support indexing (through the IndexView
trait):
let arr = array![1, 2, 3];
let three = arr[2]; // Returns a snapshot (@T)
Arrays can be converted to Span
s for read-only access:
let arr = array![1, 2, 3];
let span = arr.span();
A span can be manipulated without affecting the original array:
let mut arr = array![1, 2, 3];
let mut span = arr.span();
span.pop_back();
assert!(arr == array![1, 2, 3]);
Fully qualified path: core::array