Array

Trait Array 

Source
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§

Source

fn len(&self) -> usize

Returns the number of elements in the array.

Source

fn is_empty(&self) -> bool

Returns true if the array has a length of 0

Source

fn first(&self) -> Option<&T>

Returns the first element of the array, or None if it is empty.

Source

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.

Source

fn last(&self) -> Option<&T>

Returns the last element of the array, or None if it is empty.

Source

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.

Source

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.

Source

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.

Source

fn as_slice(&self) -> &[T]

Extracts a slice containing the entire array.

Source

fn as_mut_slice(&mut self) -> &mut [T]

Extracts a mutable slice of the entire array.

Source

fn map_<F>(self, f: F) -> Self
where F: FnMut(T) -> T, Self: Sized,

Takes a FnMut(T) -> T closure and creates a new array by calling that closure on each element.

Source

fn foldl<A, F>(self, acc: A, f: F) -> A
where F: FnMut(A, T) -> A, Self: Sized,

Applies a function over the entire array, producing a single final value.

Source

fn foldr<A, F>(self, acc: A, f: F) -> A
where F: FnMut(A, T) -> A, Self: Sized,

Applies a function over the entire array (in reverse order), producing a single final value.

Source

fn resize<const S: usize>(self, elem: T) -> [T; S]
where T: Clone, Self: Sized,

Resizes the array, filling new spaces at the end with the specified element.

Source

fn resize_with<F, const S: usize>(self, f: F) -> [T; S]
where F: FnMut(usize) -> T, Self: Sized,

Resizes the array, filling new spaces at the end with the values generated from a function.

Source

fn from_fn<F>(f: F) -> Self
where F: FnMut(usize) -> T, Self: Sized,

👎Deprecated since 0.4.0: use std::array::from_fn instead

Creates a new array using the provided closure.

Source

fn from_iter(iter: impl Iterator<Item = T>) -> Option<Self>
where Self: Sized,

Creates an array by extracting elements from the provided iterator.

Implementations on Foreign Types§

Source§

impl<T, const N: usize> Array<T> for [T; N]

Source§

fn len(&self) -> usize

Source§

fn is_empty(&self) -> bool

Source§

fn first(&self) -> Option<&T>

Source§

fn first_mut(&mut self) -> Option<&mut T>

Source§

fn last(&self) -> Option<&T>

Source§

fn last_mut(&mut self) -> Option<&mut T>

Source§

fn get(&self, index: usize) -> Option<&T>

Source§

fn get_mut(&mut self, index: usize) -> Option<&mut T>

Source§

fn as_slice(&self) -> &[T]

Source§

fn as_mut_slice(&mut self) -> &mut [T]

Source§

fn map_<F>(self, f: F) -> Self
where F: FnMut(T) -> T,

Source§

fn foldl<A, F>(self, acc: A, f: F) -> A
where F: FnMut(A, T) -> A,

Source§

fn foldr<A, F>(self, acc: A, f: F) -> A
where F: FnMut(A, T) -> A,

Source§

fn resize<const S: usize>(self, elem: T) -> [T; S]
where T: Clone,

Source§

fn resize_with<F, const S: usize>(self, f: F) -> [T; S]
where F: FnMut(usize) -> T,

Source§

fn from_fn<F>(f: F) -> Self
where F: FnMut(usize) -> T,

👎Deprecated since 0.4.0: use std::array::from_fn instead
Source§

fn from_iter(iter: impl Iterator<Item = T>) -> Option<Self>

Implementors§