pub trait ExtSlice<T>: Sealed {
// Required methods
fn slice_lsplit(&self, len: usize) -> &[T] ⓘ;
fn slice_rsplit(&self, len: usize) -> &[T] ⓘ;
fn slice_msplit_left(&self, len: usize) -> &[T] ⓘ;
fn slice_msplit_right(&self, len: usize) -> &[T] ⓘ;
fn slice_into_array<U, const N: usize>(&self) -> [U; N]
where T: Clone,
U: From<T>;
fn slice_into_vec<U>(&self) -> Vec<U> ⓘ
where T: Clone,
U: From<T>;
fn slice_try_into_vec<E, U>(&self) -> Result<Vec<U>, E> ⓘ
where T: Clone,
U: TryFrom<T, Error = E>;
}
Expand description
Extension trait providing additional methods for &[T]
.
This trait is sealed and cannot be implemented for any other type.
Required Methods§
Sourcefn slice_lsplit(&self, len: usize) -> &[T] ⓘ
fn slice_lsplit(&self, len: usize) -> &[T] ⓘ
Returns a left subslice of slice
with the given maximum len
.
If left_len > slice.len()
it returns the full slice.
See also Slice::lsplit
for the standalone const
version.
Sourcefn slice_rsplit(&self, len: usize) -> &[T] ⓘ
fn slice_rsplit(&self, len: usize) -> &[T] ⓘ
Returns a right subslice of slice
with the given maximum len
.
If left_len > slice.len()
it returns the full slice.
See also Slice::rsplit
for the standalone const
version.
Sourcefn slice_msplit_left(&self, len: usize) -> &[T] ⓘ
fn slice_msplit_left(&self, len: usize) -> &[T] ⓘ
Returns a middle subslice of slice
with the given maximum len
and a left bias.
In case of a non-perfect middle split, it will have one character more on the left.
If len > slice.len()
returns the full slice
.
See also Slice::msplit_left
for the standalone const
version.
Sourcefn slice_msplit_right(&self, len: usize) -> &[T] ⓘ
fn slice_msplit_right(&self, len: usize) -> &[T] ⓘ
Returns a middle subslice of slice
with the given maximum len
and a right bias.
In case of a non-perfect middle split, it will have one character more on the right.
If len > slice.len()
returns the full slice
.
See also Slice::msplit_right
for the standalone const
version.
Sourcefn slice_into_array<U, const N: usize>(&self) -> [U; N]
fn slice_into_array<U, const N: usize>(&self) -> [U; N]
Converts &[T]
to [U; N]
when U
implements From<T>
.
§Panics
Panics if the length of the slice is less than the length of the array.
§Examples
assert_eq![[1_u16, 2, 3], [1_u8, 2, 3].slice_into_array::<u16, 3>()];
assert_eq![[1_u16, 2, 3], [1_u8, 2, 3].slice_into_array::<u16, 3>()];
§Features
If the unsafe_array
feature is enabled it uses MaybeUninit
to improve performance.
Sourcefn slice_into_vec<U>(&self) -> Vec<U> ⓘ
Available on crate feature alloc
only.
fn slice_into_vec<U>(&self) -> Vec<U> ⓘ
alloc
only.Converts &[T]
to Vec<U>
when U
implements From<T>
.
§Examples
assert_eq![vec![1_i16, 2, 3], [1_u8, 2, 3].slice_into_vec::<i16>()];
assert_eq![vec![1_i16, 2, 3], [1_u8, 2, 3].slice_into_vec::<i16>()];
Sourcefn slice_try_into_vec<E, U>(&self) -> Result<Vec<U>, E> ⓘ
Available on crate feature alloc
only.
fn slice_try_into_vec<E, U>(&self) -> Result<Vec<U>, E> ⓘ
alloc
only.Tries to convert &[T]
to Vec<U>
when U
implements TryFrom<T>
.
§Examples
assert_eq![Ok(vec![1_i32, 2, 3]), [1_i64, 2, 3].slice_try_into_vec()];
assert_eq![Ok(vec![1_i32, 2, 3]), [1_i64, 2, 3].slice_try_into_vec::<_, i32>()];
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<T> ExtSlice<T> for &[T]
impl<T> ExtSlice<T> for &[T]
fn slice_lsplit(&self, len: usize) -> &[T] ⓘ
fn slice_rsplit(&self, len: usize) -> &[T] ⓘ
fn slice_msplit_left(&self, len: usize) -> &[T] ⓘ
fn slice_msplit_right(&self, len: usize) -> &[T] ⓘ
fn slice_into_array<U, const N: usize>(&self) -> [U; N]
Source§fn slice_into_vec<U>(&self) -> Vec<U> ⓘ
fn slice_into_vec<U>(&self) -> Vec<U> ⓘ
alloc
only.Source§impl<T> ExtSlice<T> for &mut [T]
impl<T> ExtSlice<T> for &mut [T]
fn slice_lsplit(&self, len: usize) -> &[T] ⓘ
fn slice_rsplit(&self, len: usize) -> &[T] ⓘ
fn slice_msplit_left(&self, len: usize) -> &[T] ⓘ
fn slice_msplit_right(&self, len: usize) -> &[T] ⓘ
fn slice_into_array<U, const N: usize>(&self) -> [U; N]
Source§fn slice_into_vec<U>(&self) -> Vec<U> ⓘ
fn slice_into_vec<U>(&self) -> Vec<U> ⓘ
alloc
only.Source§impl<T> ExtSlice<T> for [T]
impl<T> ExtSlice<T> for [T]
fn slice_lsplit(&self, len: usize) -> &[T] ⓘ
fn slice_rsplit(&self, len: usize) -> &[T] ⓘ
fn slice_msplit_left(&self, len: usize) -> &[T] ⓘ
fn slice_msplit_right(&self, len: usize) -> &[T] ⓘ
fn slice_into_array<U, const N: usize>(&self) -> [U; N]
Source§fn slice_into_vec<U>(&self) -> Vec<U> ⓘ
fn slice_into_vec<U>(&self) -> Vec<U> ⓘ
alloc
only.Source§impl<T, const LEN: usize> ExtSlice<T> for [T; LEN]
impl<T, const LEN: usize> ExtSlice<T> for [T; LEN]
fn slice_lsplit(&self, len: usize) -> &[T] ⓘ
fn slice_rsplit(&self, len: usize) -> &[T] ⓘ
fn slice_msplit_left(&self, len: usize) -> &[T] ⓘ
fn slice_msplit_right(&self, len: usize) -> &[T] ⓘ
fn slice_into_array<U, const N: usize>(&self) -> [U; N]
Source§fn slice_into_vec<U>(&self) -> Vec<U> ⓘ
fn slice_into_vec<U>(&self) -> Vec<U> ⓘ
alloc
only.