Trait slice_utils::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§
sourcetype Output
type Output
The type this slice returns; analagous to
Index::Output
.
Required Methods§
sourcefn get_with<W: FnMut(&Self::Output) -> R, R>(
&self,
index: usize,
f: &mut W,
) -> Option<R>
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§
sourcefn is_empty(&self) -> bool
fn is_empty(&self) -> bool
Returns whether or not the slice is empty.
Equivalent to slice.len() == 0
.
sourcefn chain<S: Slice<Output = Self::Output>>(self, other: S) -> Chain<Self, S>where
Self: Sized,
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]);
sourcefn cycle(self) -> Cycle<Self>where
Self: Sized,
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));
sourcefn interleave<S: Slice<Output = Self::Output>>(
self,
other: S,
) -> Interleave<Self, S>where
Self: Sized,
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]);
sourcefn rev(self) -> Reverse<Self>where
Self: Sized,
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]);
sourcefn slice<R: RangeBounds<usize>>(self, range: R) -> Option<SliceOf<Self>>where
Self: Sized,
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]);
sourcefn split(&self, at: usize) -> Option<(SliceOf<&Self>, SliceOf<&Self>)>
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]);
Object Safety§
This trait is not object safe.