Primitive type array
Implementations
impl<let N: u32, T> [T; N]
impl<let N: u32, T> [T; N]
pub fn len(self) -> u32
Returns the length of this array.
fn len(self) -> Field
example
fn main() {
let array = [42, 42];
assert(array.len() == 2);
}
pub fn as_slice(self) -> [T]
Returns this array as a slice.
let array = [1, 2];
let slice = array.as_slice();
assert_eq(slice, &[1, 2]);
pub fn map<U, Env>(self, f: fn[Env](T) -> U) -> [U; N]
Applies a function to each element of this array, returning a new array containing the mapped elements.
Example:
let a = [1, 2, 3];
let b = a.map(|a| a * 2);
assert_eq(b, [2, 4, 6]);
pub fn mapi<U, Env>(self, f: fn[Env](u32, T) -> U) -> [U; N]
Applies a function to each element of this array along with its index, returning a new array containing the mapped elements.
Example:
let a = [1, 2, 3];
let b = a.mapi(|i, a| i + a * 2);
assert_eq(b, [2, 5, 8]);
pub fn for_each<Env>(self, f: fn[Env](T))
Applies a function to each element of this array.
Example:
let a = [1, 2, 3];
let mut b = [0; 3];
let mut i = 0;
a.for_each(|x| {
b[i] = x;
i += 1;
});
assert_eq(a, b);
pub fn for_eachi<Env>(self, f: fn[Env](u32, T))
Applies a function to each element of this array along with its index.
Example:
let a = [1, 2, 3];
let mut b = [0; 3];
a.for_eachi(|i, x| {
b[i] = x;
});
assert_eq(a, b);
pub fn fold<U, Env>(self, accumulator: U, f: fn[Env](U, T) -> U) -> U
Applies a function to each element of the array, returning the final accumulated value. The first parameter is the initial value.
This is a left fold, so the given function will be applied to the accumulator and first element of the array, then the second, and so on. For a given call the expected result would be equivalent to:
let a1 = [1];
let a2 = [1, 2];
let a3 = [1, 2, 3];
let f = |a, b| a - b;
a1.fold(10, f); //=> f(10, 1)
a2.fold(10, f); //=> f(f(10, 1), 2)
a3.fold(10, f); //=> f(f(f(10, 1), 2), 3)
assert_eq(a3.fold(10, f), 10 - 1 - 2 - 3);
pub fn reduce<Env>(self, f: fn[Env](T, T) -> T) -> T
Same as fold, but uses the first element as the starting element.
Requires the input array to be non-empty.
Example:
fn main() {
let arr = [1, 2, 3, 4];
let reduced = arr.reduce(|a, b| a + b);
assert(reduced == 10);
}
pub fn all<Env>(self, predicate: fn[Env](T) -> bool) -> bool
Returns true if all the elements in this array satisfy the given predicate.
Example:
fn main() {
let arr = [2, 2, 2, 2, 2];
let all = arr.all(|a| a == 2);
assert(all);
}
pub fn any<Env>(self, predicate: fn[Env](T) -> bool) -> bool
Returns true if any of the elements in this array satisfy the given predicate.
Example:
fn main() {
let arr = [2, 2, 2, 2, 5];
let any = arr.any(|a| a == 5);
assert(any);
}
pub fn concat<let M: u32>(self, array2: [T; M]) -> [T; N + M]
Concatenates this array with another array.
Example:
fn main() {
let arr1 = [1, 2, 3, 4];
let arr2 = [6, 7, 8, 9, 10, 11];
let concatenated_arr = arr1.concat(arr2);
assert(concatenated_arr == [1, 2, 3, 4, 6, 7, 8, 9, 10, 11]);
}
impl<let N: u32> [u8; N]
pub fn as_str_unchecked(self) -> str<N>
Converts a byte array of type [u8; N] to a string. Note that this performs no UTF-8 validation -
the given array is interpreted as-is as a string.
Example:
fn main() {
let hi = [104, 105].as_str_unchecked();
assert_eq(hi, "hi");
}
impl<let N: u32, T> [T; N]
pub fn sort(self) -> Self
Returns a new sorted array. The original array remains untouched. Notice that this function will
only work for arrays of fields or integers, not for any arbitrary type. This is because the sorting
logic it uses internally is optimized specifically for these values. If you need a sort function to
sort any type, you should use the sort_via function.
Example:
fn main() {
let arr = [42, 32];
let sorted = arr.sort();
assert(sorted == [32, 42]);
}
Returns a new sorted array by sorting it with a custom comparison function. The original array remains untouched. The ordering function must return true if the first argument should be sorted to be before the second argument or is equal to the second argument.
Using this method with an operator like
<that does not returntruefor equal values will result in an assertion failure for arrays with equal elements.Example: