Trait Slice

Source
pub trait Slice {
    type Output;

    // Required methods
    fn len(&self) -> usize;
    fn get_with<W: FnMut(&Self::Output) -> R, R>(
        &self,
        index: usize,
        f: &mut W,
    ) -> Option<R>;

    // Provided methods
    fn is_empty(&self) -> bool { ... }
    fn chain<S: Slice<Output = Self::Output>>(self, other: S) -> Chain<Self, S>
       where Self: Sized { ... }
    fn cycle(self) -> Cycle<Self>
       where Self: Sized { ... }
    fn interleave<S: Slice<Output = Self::Output>>(
        self,
        other: S,
    ) -> Interleave<Self, S>
       where Self: Sized { ... }
    fn rev(self) -> Reverse<Self>
       where Self: Sized { ... }
    fn slice<R: RangeBounds<usize>>(self, range: R) -> Option<SliceOf<Self>>
       where Self: Sized { ... }
    fn split(&self, at: usize) -> Option<(SliceOf<&Self>, SliceOf<&Self>)> { ... }
}
Expand description

The base trait for SliceOwned, SliceBorrowed, and SliceMut.

Required Associated Types§

Source

type Output

The type this slice returns; analagous to Index::Output.

Required Methods§

Source

fn len(&self) -> usize

Returns the length of the slice.

Source

fn get_with<W: FnMut(&Self::Output) -> R, R>( &self, index: usize, f: &mut W, ) -> Option<R>

Call a closure with the indicated element, returning the result or None if the index was out-of-bounds.

This allows operations that are independent of indexing method.

§Examples
let a = [1_i32, 2, 3];

// Doesn't care about ownership
fn stringify_first<S>(slice: &S) -> String
where
    S: Slice,
    S::Output: ToString,
{
    slice.get_with(0, &mut |x| x.to_string()).unwrap_or_default()
}

assert_eq!(stringify_first(&a), "1");

Provided Methods§

Source

fn is_empty(&self) -> bool

Returns whether or not the slice is empty.

Equivalent to slice.len() == 0.

Source

fn chain<S: Slice<Output = Self::Output>>(self, other: S) -> Chain<Self, S>
where Self: Sized,

Chains two slices together, back-to-back.

Analagous to Iterator::chain.

§Examples
let a = [1, 2, 3];
let b = [4, 5, 6];

assert_eq!(a.chain(b), [1, 2, 3, 4, 5, 6]);
Source

fn cycle(self) -> Cycle<Self>
where Self: Sized,

Cycles the slice infinitely.

Analagous to Iterator::cycle.

§Examples
let slice = [1, 2, 3].cycle();
assert_eq!(slice.get_owned(2), Some(3));
assert_eq!(slice.get_owned(4), Some(2));
assert_eq!(slice.get_owned(6), Some(1));
Source

fn interleave<S: Slice<Output = Self::Output>>( self, other: S, ) -> Interleave<Self, S>
where Self: Sized,

Interleaves two slices, e.g. [A, B, A, B, …].

§Examples
let a = [1, 2, 3];
let b = [4, 5, 6];
let c = a.interleave(b);

assert_eq!(c, [1, 4, 2, 5, 3, 6]);
Source

fn rev(self) -> Reverse<Self>
where Self: Sized,

Reverses the slice.

Analagous to Iterator::rev.

§Examples
let slice = [1, 2, 3].rev();
assert_eq!(slice, [3, 2, 1]);
Source

fn slice<R: RangeBounds<usize>>(self, range: R) -> Option<SliceOf<Self>>
where Self: Sized,

Create a sub-slice of the slice.

Analagous to slicing &[T].

§Examples
let slice = [1, 2, 3, 4, 5].slice(1..4).unwrap();
assert_eq!(slice, [2, 3, 4]);
Source

fn split(&self, at: usize) -> Option<(SliceOf<&Self>, SliceOf<&Self>)>

Returns (&self[..at], &self[at..]). Returns None if at is out-of-bounds.

Analagous to slice::split.

§Examples
let slice = [1, 2, 3, 4, 5, 6];
let (a, b) = slice.split(3).unwrap();

assert_eq!(a, [1, 2, 3]);
assert_eq!(b, [4, 5, 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.

Implementations on Foreign Types§

Source§

impl<'a, S> Slice for &'a S
where S: Slice + ?Sized,

Source§

type Output = <S as Slice>::Output

Source§

fn len(&self) -> usize

Source§

fn get_with<W: FnMut(&Self::Output) -> R, R>( &self, index: usize, f: &mut W, ) -> Option<R>

Source§

impl<'a, S> Slice for &'a mut S
where S: Slice + ?Sized,

Source§

type Output = <S as Slice>::Output

Source§

fn len(&self) -> usize

Source§

fn get_with<W: FnMut(&Self::Output) -> R, R>( &self, index: usize, f: &mut W, ) -> Option<R>

Source§

impl<T> Slice for [T]

Source§

type Output = T

Source§

fn len(&self) -> usize

Source§

fn get_with<W: FnMut(&Self::Output) -> R, R>( &self, index: usize, f: &mut W, ) -> Option<R>

Source§

impl<T> Slice for Range<T>
where T: Clone, Range<T>: Iterator<Item = T>,

Source§

type Output = T

Source§

fn len(&self) -> usize

Source§

fn get_with<W: FnMut(&Self::Output) -> R, R>( &self, index: usize, f: &mut W, ) -> Option<R>

Source§

impl<T> Slice for RangeFrom<T>
where T: Clone, RangeFrom<T>: Iterator<Item = T>,

Source§

type Output = T

Source§

fn len(&self) -> usize

Source§

fn get_with<W: FnMut(&Self::Output) -> R, R>( &self, index: usize, f: &mut W, ) -> Option<R>

Source§

impl<T> Slice for RangeInclusive<T>
where T: Clone, RangeInclusive<T>: Iterator<Item = T>,

Source§

type Output = T

Source§

fn len(&self) -> usize

Source§

fn get_with<W: FnMut(&Self::Output) -> R, R>( &self, index: usize, f: &mut W, ) -> Option<R>

Source§

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

Source§

type Output = T

Source§

fn len(&self) -> usize

Source§

fn get_with<W: FnMut(&Self::Output) -> R, R>( &self, index: usize, f: &mut W, ) -> Option<R>

Implementors§

Source§

impl<'a, S> Slice for WindowsBorrowed<'a, S>
where S: SliceBorrowed + ?Sized,

Source§

impl<'a, S> Slice for WindowsOwned<'a, S>
where S: SliceOwned + ?Sized,

Source§

impl<'a, S, const N: usize> Slice for ArrayWindowsBorrowed<'a, S, N>
where S: SliceBorrowed + ?Sized,

Source§

type Output = [&'a <S as Slice>::Output; N]

Source§

impl<A> Slice for SliceOf<A>
where A: Slice,

Source§

type Output = <A as Slice>::Output

Source§

impl<A> Slice for SplitMut<'_, A>
where A: Slice + ?Sized,

Source§

type Output = <A as Slice>::Output

Source§

impl<F, T> Slice for FromFn<F>
where F: Fn(usize) -> Option<T>,

Source§

impl<S1, S2> Slice for Chain<S1, S2>
where S1: Slice, S2: Slice<Output = S1::Output>,

Source§

type Output = <S1 as Slice>::Output

Source§

impl<S1, S2> Slice for Interleave<S1, S2>
where S1: Slice, S2: Slice<Output = S1::Output>,

Source§

type Output = <S1 as Slice>::Output

Source§

impl<S1, S2> Slice for Zip<S1, S2>
where S1: SliceOwned, S2: SliceOwned,

Source§

type Output = (<S1 as Slice>::Output, <S2 as Slice>::Output)

Source§

impl<S> Slice for Cycle<S>
where S: Slice,

Source§

type Output = <S as Slice>::Output

Source§

impl<S> Slice for Reverse<S>
where S: Slice,

Source§

type Output = <S as Slice>::Output

Source§

impl<S, F, U> Slice for MapBorrowed<S, F>
where S: SliceBorrowed, F: Fn(&S::Output) -> U,

Source§

impl<S, F, U> Slice for MapOwned<S, F>
where S: SliceOwned, F: Fn(S::Output) -> U,

Source§

impl<S, const N: usize> Slice for ArrayWindowsOwned<S, N>
where S: SliceOwned + Sized,

Source§

type Output = [<S as Slice>::Output; N]