Trait slice_utils::Slice

source ·
pub trait Slice<T>: Sized {
    // Required methods
    fn get(&self, index: usize) -> Option<&T>;
    fn len(&self) -> usize;

    // Provided methods
    fn is_empty(&self) -> bool { ... }
    fn slice<R: RangeBounds<usize>>(self, range: R) -> Option<SliceOf<T, Self>> { ... }
    fn skip(self, n: usize) -> Option<SliceOf<T, Self>> { ... }
    fn take(self, n: usize) -> Option<SliceOf<T, Self>> { ... }
    fn chain<O: Slice<T>>(self, other: O) -> Chain<T, Self, O> { ... }
    fn cycle(self) -> Cycle<T, Self> { ... }
    fn interleave<O: Slice<T>>(self, other: O) -> Interleave<T, Self, O> { ... }
    fn reverse(self) -> Reverse<T, Self> { ... }
    fn windows(&self, size: usize) -> Windows<'_, T, Self>  { ... }
    fn split(&self, at: usize) -> Option<SplitOf<T, &Self>> { ... }
}
Expand description

An extension trait providing iterator-like utilities for slices.

Required Methods§

source

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

Index the slice.

If index < self.len(), this must succeed.

§Examples
let slice = [1, 2, 3];
assert_eq!(slice.get(2), Some(&3));
source

fn len(&self) -> usize

Returns the exact length of the slice.

If infinite (e.g. through [cycle]), returns usize::MAX.

§Examples
let slice = [1, 2, 3];
assert_eq!(slice.len(), 3);
let inf = slice.cycle();
assert_eq!(inf.len(), usize::MAX);

Provided Methods§

source

fn is_empty(&self) -> bool

Whether or not the slice is empty (self.len() == 0).

§Examples
assert!([0; 0].is_empty());
assert!(![1, 2, 3].is_empty());
source

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

Takes a sub-slice, returning None if the range is out-of-bounds.

§Examples
let slice = [1, 2, 3, 4, 5];
assert_eq!(slice.slice(1..4).unwrap(), [2, 3, 4]);
assert_eq!(slice.slice(0..6), None);
Examples found in repository?
examples/sliding_count.rs (line 8)
3
4
5
6
7
8
9
10
11
12
13
14
15
fn main() {
    let a = [1, 0, 0, 1, 1];
    let b = [2, 1, 1, 2, 3];

    a.cycle()
        .slice(4..11)
        .unwrap()
        .windows(3)
        .map(|x| x[0] + x[1] + x[2])
        .for_each(|x| println!(" - {x:?}"));

    dbg!(b);
}
source

fn skip(self, n: usize) -> Option<SliceOf<T, Self>>

Shortcut for .slice(n + 1..)

§Examples
let slice = [1, 2, 3, 4];
assert_eq!(slice.skip(2), slice.slice(3..));
source

fn take(self, n: usize) -> Option<SliceOf<T, Self>>

Shorcut for .slice(..=n).

§Examples
let slice = [1, 2, 3, 4];
assert_eq!(slice.take(2), slice.slice(..=2));
source

fn chain<O: Slice<T>>(self, other: O) -> Chain<T, Self, O>

Chains two slices together, back-to-back: the equivalent of 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<T, Self>

Repeats the slice forever: the equivalent of Iterator::cycle.

§Examples
let slice = [1, 2, 3].cycle();

assert_eq!(slice[2], 3);
assert_eq!(slice[4], 2);
assert_eq!(slice[6], 1);
Examples found in repository?
examples/sliding_count.rs (line 7)
3
4
5
6
7
8
9
10
11
12
13
14
15
fn main() {
    let a = [1, 0, 0, 1, 1];
    let b = [2, 1, 1, 2, 3];

    a.cycle()
        .slice(4..11)
        .unwrap()
        .windows(3)
        .map(|x| x[0] + x[1] + x[2])
        .for_each(|x| println!(" - {x:?}"));

    dbg!(b);
}
source

fn interleave<O: Slice<T>>(self, other: O) -> Interleave<T, Self, O>

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 reverse(self) -> Reverse<T, Self>

Reverses the slice: the equivalent of Iterator::rev.

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

fn windows(&self, size: usize) -> Windows<'_, T, Self>

Returns an iterator over overlapping slices of length size: the equivalent of slice::windows.

§Examples
let slice = [1, 2, 3, 4, 5];
let mut w = slice.windows(3);

assert_eq!(w.next().unwrap(), [1, 2, 3]);
assert_eq!(w.next().unwrap(), [2, 3, 4]);
assert_eq!(w.next().unwrap(), [3, 4, 5]);
assert!(w.next().is_none());
Examples found in repository?
examples/sliding_count.rs (line 10)
3
4
5
6
7
8
9
10
11
12
13
14
15
fn main() {
    let a = [1, 0, 0, 1, 1];
    let b = [2, 1, 1, 2, 3];

    a.cycle()
        .slice(4..11)
        .unwrap()
        .windows(3)
        .map(|x| x[0] + x[1] + x[2])
        .for_each(|x| println!(" - {x:?}"));

    dbg!(b);
}
source

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

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

Equivalent of 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]);

Object Safety§

This trait is not object safe.

Implementations on Foreign Types§

source§

impl<'a, T> Slice<T> for &'a [T]

source§

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

source§

fn len(&self) -> usize

source§

impl<'a, T> Slice<T> for &'a mut [T]

source§

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

source§

fn len(&self) -> usize

source§

impl<'a, T, A> Slice<T> for &'a A
where A: Slice<T>,

source§

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

source§

fn len(&self) -> usize

source§

impl<'a, T, A> Slice<T> for &'a mut A
where A: Slice<T>,

source§

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

source§

fn len(&self) -> usize

source§

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

source§

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

source§

fn len(&self) -> usize

Implementors§

source§

impl<T, A> Slice<T> for Cycle<T, A>
where A: Slice<T>,

source§

impl<T, A> Slice<T> for Reverse<T, A>
where A: Slice<T>,

source§

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

source§

impl<T, A> Slice<T> for SliceOfMut<T, A>
where A: SliceMut<T>,

source§

impl<T, A, B> Slice<T> for Chain<T, A, B>
where A: Slice<T>, B: Slice<T>,

source§

impl<T, A, B> Slice<T> for Interleave<T, A, B>
where A: Slice<T>, B: Slice<T>,