Skip to content

Array

A fixed-size, indexable sequence of elements. Equivalent to Go’s [N]T.

The size is part of the type, so Array<byte, 2> and Array<byte, 4> are distinct types.

A list literal is a Slice by default, so an Array needs the annotation.

let rgb: Array<int, 3> = [255, 128, 0]Array<int, 3>
let growable = [255, 128, 0]Slice<int>

Compare to Slice

Creates an array of N zero values.

impl<T> Array<T> {
fn new() -> Array<T, N>
}
let scores = Array.new<int, 3>()[0, 0, 0]

Copies a Slice into a new Array, or returns None when the Slice does not hold exactly N elements.

impl<T> Array<T> {
fn from(slice: Slice<T>) -> Option<Array<T, N>>
}
let parts = [1, 2, 3]
let fixed = Array.from<int, 3>(parts)Some([1, 2, 3])

Returns the size of the Array.

impl<T> Array<T> {
fn length(self) -> int
}
let rgb: Array<int, 3> = [255, 128, 0]
let size = rgb.length()3

Returns the element at index, or None if out of bounds.

impl<T> Array<T> {
fn get(self, index: int) -> Option<T>
}
let rgb: Array<int, 3> = [255, 128, 0]
let green = rgb.get(1)
Some(128), or None when out of bounds

Copies the elements of the Array into a new Slice.

impl<T> Array<T> {
fn to_slice(self) -> mut Slice<T>
}
let parts: Array<string, 3> = ["a", "b", "c"]
let path = strings.Join(parts.to_slice(), "-")
strings.Join takes a Slice, not an Array