Trait ArrayShorthand

Source
pub trait ArrayShorthand: Array {
    // Provided methods
    fn index<Idx>(&self, idx: Idx) -> &Idx::Output
       where Idx: SliceIndex<[Self::Item]> { ... }
    fn index_mut<Idx>(&mut self, idx: Idx) -> &mut Idx::Output
       where Idx: SliceIndex<[Self::Item]> { ... }
    fn replace(&mut self, idx: usize, item: Self::Item) -> Self::Item { ... }
    fn take(&mut self, idx: usize) -> Self::Item
       where Self::Item: Default { ... }
    fn iter(&self) -> Iter<'_, Self::Item> { ... }
    fn iter_mut(&mut self) -> IterMut<'_, Self::Item> { ... }
}
Expand description

Shorthand methods those just refer to self.as_slice().smt()

Provided Methods§

Source

fn index<Idx>(&self, idx: Idx) -> &Idx::Output
where Idx: SliceIndex<[Self::Item]>,

Returns a reference to the value corresponding to the supplied index.

§Example
use arraylib::{Array, ArrayShorthand};

fn slice<A>(arr: &A) -> (&A::Item, &[A::Item])
where
    A: Array,
{
    (arr.index(0), arr.index(1..))
}

assert_eq!(slice(&[1, 2, 3]), (&1, &[2, 3][..]))

See also: index_mut

Source

fn index_mut<Idx>(&mut self, idx: Idx) -> &mut Idx::Output
where Idx: SliceIndex<[Self::Item]>,

Returns a unique reference to the value corresponding to the supplied index.

§Example
use arraylib::{Array, ArrayShorthand};

fn slice<A>(arr: &mut A) -> &mut [A::Item]
where
    A: Array,
{
    arr.index_mut(1..)
}

assert_eq!(slice(&mut [1, 2, 3]), &mut [2, 3])

See also: index

Source

fn replace(&mut self, idx: usize, item: Self::Item) -> Self::Item

Replace element of the array

§Example
use arraylib::ArrayShorthand;

let mut arr = ["hello", "", "world"];
assert_eq!(arr.replace(1, ", "), "");
assert_eq!(arr, ["hello", ", ", "world"])
Source

fn take(&mut self, idx: usize) -> Self::Item
where Self::Item: Default,

Take element of the array, replacing it with default

§Example
use arraylib::ArrayShorthand;

let mut arr = [String::from("hello, "), String::from("world")];
assert_eq!(arr.take(1), "world");
assert_eq!(arr, ["hello, ", ""])
Source

fn iter(&self) -> Iter<'_, Self::Item>

Returns an iterator over refs to the array.

§Examples
use arraylib::ArrayShorthand;

let arr = [1, 2, 4];
let mut iterator = arr.iter();

assert_eq!(iterator.next(), Some(&1));
assert_eq!(iterator.next(), Some(&2));
assert_eq!(iterator.next(), Some(&4));
assert_eq!(iterator.next(), None);
Source

fn iter_mut(&mut self) -> IterMut<'_, Self::Item>

Returns an iterator that allows modifying each value.

§Examples
use arraylib::ArrayShorthand;

let mut arr = [1, 2, 4];
for elem in arr.iter_mut() {
    *elem += 2;
}
assert_eq!(arr, [3, 4, 6]);

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.

Implementors§

Source§

impl<A> ArrayShorthand for A
where A: Array,