Trait slice_utils::Slice
source · pub trait Slice<T>: Sized {
Show 14 methods
// 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 chunks(&self, size: usize) -> Chunks<'_, T, Self> ⓘ { ... }
fn chunks_exact(&self, size: usize) -> Option<Chunks<'_, T, Self>> { ... }
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§
Provided Methods§
sourcefn is_empty(&self) -> bool
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());sourcefn slice<R: RangeBounds<usize>>(self, range: R) -> Option<SliceOf<T, Self>>
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?
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);
}sourcefn skip(self, n: usize) -> Option<SliceOf<T, Self>>
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..));sourcefn take(self, n: usize) -> Option<SliceOf<T, Self>>
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));sourcefn chain<O: Slice<T>>(self, other: O) -> Chain<T, Self, O>
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]);sourcefn chunks(&self, size: usize) -> Chunks<'_, T, Self> ⓘ
fn chunks(&self, size: usize) -> Chunks<'_, T, Self> ⓘ
Returns an iterator over fixed-size chunks: the equivalent of
slice::chunks. The last chunk may be smaller than the rest.
§Examples
let slice = [1, 2, 3, 4, 5];
let mut chunks = slice.chunks(2);
assert_eq!(chunks.next().unwrap(), [1, 2]);
assert_eq!(chunks.next().unwrap(), [3, 4]);
assert_eq!(chunks.next().unwrap(), [5]);
assert!(chunks.next().is_none());sourcefn chunks_exact(&self, size: usize) -> Option<Chunks<'_, T, Self>>
fn chunks_exact(&self, size: usize) -> Option<Chunks<'_, T, Self>>
Returns an iterator over fixed-size chunks. If data.len() is not
divisible by size, returns None. Otherwise, equivalent to [chunks].
§Examples
let slice = [1, 2, 3, 4, 5];
assert!(slice.chunks_exact(2).is_none());sourcefn cycle(self) -> Cycle<T, Self>
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?
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);
}sourcefn interleave<O: Slice<T>>(self, other: O) -> Interleave<T, Self, O>
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]);sourcefn reverse(self) -> Reverse<T, Self>
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]);sourcefn windows(&self, size: usize) -> Windows<'_, T, Self> ⓘ
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?
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);
}