ArrayTools

Trait ArrayTools 

Source
pub trait ArrayTools<T, const N: usize> {
Show 13 methods // Required methods fn skip<const BY: usize>(self) -> [T; { _ }]; fn step<const STEP: usize>(self) -> [T; { _ }]; fn intersperse(self, with: T) -> [T; { _ }] where T: Clone; fn each(self, apply: impl FnMut(T)); fn enumerate(self) -> [(T, usize); N]; fn windowed<const W: usize>(&self) -> [&[T; W]; { _ }]; fn inspect(self, f: impl FnMut(&T)) -> Self; fn rev(self) -> Self; fn interleave(self, with: [T; N]) -> [T; { _ }]; fn cartesian_product<U: Clone, const M: usize>( self, with: &[U; M], ) -> [(T, U); { _ }] where T: Clone; fn sort(self) -> Self where T: Ord; fn sum(self) -> T where T: Sum<T>; fn product(self) -> T where T: Product<T>;
}
Expand description

Array tools.

Required Methods§

Source

fn skip<const BY: usize>(self) -> [T; { _ }]

Skip BY elements.

Source

fn step<const STEP: usize>(self) -> [T; { _ }]

Skip every BY elements.

let x = range::<5>().step::<2>();
assert_eq!(x, [0, 2, 4]);
let x = range::<20>().step::<5>();
assert_eq!(x, [0, 5, 10, 15]);
assert_eq!(range::<50>().step::<3>(), [0, 3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33, 36, 39, 42, 45, 48]);
Source

fn intersperse(self, with: T) -> [T; { _ }]
where T: Clone,

Intersperse a element in between items.

let x = range::<3>().intersperse(5);
assert_eq!(x, [0, 5, 1, 5, 2]);
Source

fn each(self, apply: impl FnMut(T))

Run a function on every element.

Source

fn enumerate(self) -> [(T, usize); N]

Embed the index.

Source

fn windowed<const W: usize>(&self) -> [&[T; W]; { _ }]

Get the sliding windows of this array.

assert_eq!(range::<5>().windowed::<2>(), [&[0, 1], &[1, 2], &[2, 3], &[3, 4]]);
Source

fn inspect(self, f: impl FnMut(&T)) -> Self

Inspect every element of this array.

Source

fn rev(self) -> Self

Reverse this array.

Source

fn interleave(self, with: [T; N]) -> [T; { _ }]

Interleave items from two arrays.

assert_eq!([0u8, 2, 4].interleave([1, 3, 5]), [0, 1, 2, 3, 4, 5]);
Source

fn cartesian_product<U: Clone, const M: usize>( self, with: &[U; M], ) -> [(T, U); { _ }]
where T: Clone,

Cartesian product (A  ×  B) of two arrays.

assert_eq!([1u64, 2].cartesian_product(&["Π", "Σ"]), [(1, "Π"), (1, "Σ"), (2, "Π"), (2, "Σ")]);
Source

fn sort(self) -> Self
where T: Ord,

Sorts it. This uses [T]::sort_unstable.

Source

fn sum(self) -> T
where T: Sum<T>,

Sum of the array.

Source

fn product(self) -> T
where T: Product<T>,

Product of the array.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

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

Source§

fn skip<const BY: usize>(self) -> [T; { _ }]

Source§

fn step<const STEP: usize>(self) -> [T; { _ }]

Source§

fn intersperse(self, with: T) -> [T; { _ }]
where T: Clone,

Source§

fn each(self, apply: impl FnMut(T))

Source§

fn enumerate(self) -> [(T, usize); N]

Source§

fn windowed<const W: usize>(&self) -> [&[T; W]; { _ }]

Source§

fn inspect(self, f: impl FnMut(&T)) -> Self

Source§

fn rev(self) -> Self

Source§

fn interleave(self, with: [T; N]) -> [T; { _ }]

Source§

fn cartesian_product<U: Clone, const M: usize>( self, with: &[U; M], ) -> [(T, U); { _ }]
where T: Clone,

Source§

fn sort(self) -> Self
where T: Ord,

Source§

fn sum(self) -> T
where T: Sum<T>,

Source§

fn product(self) -> T
where T: Product<T>,

Implementors§