pub trait Array<T> {
Show 17 methods
// Required methods
fn len(&self) -> usize;
fn is_empty(&self) -> bool;
fn first(&self) -> Option<&T>;
fn first_mut(&mut self) -> Option<&mut T>;
fn last(&self) -> Option<&T>;
fn last_mut(&mut self) -> Option<&mut T>;
fn get(&self, index: usize) -> Option<&T>;
fn get_mut(&mut self, index: usize) -> Option<&mut T>;
fn as_slice(&self) -> &[T];
fn as_mut_slice(&mut self) -> &mut [T];
fn map_<F>(self, f: F) -> Self
where F: FnMut(T) -> T,
Self: Sized;
fn foldl<A, F>(self, acc: A, f: F) -> A
where F: FnMut(A, T) -> A,
Self: Sized;
fn foldr<A, F>(self, acc: A, f: F) -> A
where F: FnMut(A, T) -> A,
Self: Sized;
fn resize<const S: usize>(self, elem: T) -> [T; S]
where T: Clone,
Self: Sized;
fn resize_with<F, const S: usize>(self, f: F) -> [T; S]
where F: FnMut(usize) -> T,
Self: Sized;
fn from_fn<F>(f: F) -> Self
where F: FnMut(usize) -> T,
Self: Sized;
fn from_iter(iter: impl Iterator<Item = T>) -> Option<Self>
where Self: Sized;
}Expand description
Generic array type.
This trait allows passing arrays by value in a generic way without turning them into slices, so the functions get monomorphized for a specific size.
§Examples
use array_ext::Array;
fn average<T: Array<f32>>(arr: T) -> f32
{
let n = arr.len() as f32;
arr.foldl(0.0, |acc, val| acc + val) / n
}
assert!((average([8.96, 3.14, 17.9]) - 10.0).abs() < f32::EPSILON);Required Methods§
Sourcefn first_mut(&mut self) -> Option<&mut T>
fn first_mut(&mut self) -> Option<&mut T>
Returns a mutable pointer to the first element of the array, or None if it is empty.
Sourcefn last_mut(&mut self) -> Option<&mut T>
fn last_mut(&mut self) -> Option<&mut T>
Returns a mutable pointer to the last element of the array, or None if it is empty.
Sourcefn get(&self, index: usize) -> Option<&T>
fn get(&self, index: usize) -> Option<&T>
Returns the element of an array at the given index, or None if the index is out of bounds.
Sourcefn get_mut(&mut self, index: usize) -> Option<&mut T>
fn get_mut(&mut self, index: usize) -> Option<&mut T>
Returns a mutable reference to the element at the given index, or None if the index is out of bounds.
Sourcefn as_mut_slice(&mut self) -> &mut [T]
fn as_mut_slice(&mut self) -> &mut [T]
Extracts a mutable slice of the entire array.
Sourcefn map_<F>(self, f: F) -> Self
fn map_<F>(self, f: F) -> Self
Takes a FnMut(T) -> T closure and creates a new array by calling that closure on each element.
Sourcefn foldl<A, F>(self, acc: A, f: F) -> A
fn foldl<A, F>(self, acc: A, f: F) -> A
Applies a function over the entire array, producing a single final value.
Sourcefn foldr<A, F>(self, acc: A, f: F) -> A
fn foldr<A, F>(self, acc: A, f: F) -> A
Applies a function over the entire array (in reverse order), producing a single final value.
Sourcefn resize<const S: usize>(self, elem: T) -> [T; S]
fn resize<const S: usize>(self, elem: T) -> [T; S]
Resizes the array, filling new spaces at the end with the specified element.
Sourcefn resize_with<F, const S: usize>(self, f: F) -> [T; S]
fn resize_with<F, const S: usize>(self, f: F) -> [T; S]
Resizes the array, filling new spaces at the end with the values generated from a function.