pub struct Slice<T>(/* private fields */);Expand description
๐ ๐ ๏ธ Slice-related operations, most of them const.
๐ sys/mem/view
It is designed as a utility namespace and does not hold or wrap data itself. Instead, it operates on slices provided directly as arguments to its static methods.
ยงMethods
-
- range_to
(checked,
uncheckedโ ,
mut,
mut_checked,
mut_uncheckedโ )
โ
&slice[..end] - range_to_inclusive
(checked,
uncheckedโ ,
mut,
mut_checked,
mut_uncheckedโ )
โ
&slice[..=end] - range_from,
(checked,
uncheckedโ ,
mut,
mut_checked,
mut_uncheckedโ )
โ
&slice[start..] - range
(checked,
uncheckedโ ,
mut,
mut_checked,
mut_uncheckedโ )
โ
&slice[start..end] - range_inclusive
(checked,
uncheckedโ ,
mut,
mut_checked,
mut_uncheckedโ )
โ
&slice[start..=end]
- range_to
(checked,
uncheckedโ ,
mut,
mut_checked,
mut_uncheckedโ )
โ
-
- take_first
(checked,
uncheckedโ ,
mut,
mut_checked,
mut_uncheckedโ )
โ
&slice[..n] - take_last
(checked,
uncheckedโ ,
mut,
mut_checked,
mut_uncheckedโ )
โ
&slice[len - n..] - take_omit_last
(checked,
uncheckedโ
mut,
mut_checked,
mut_uncheckedโ )
โ
&slice[..len - n]
- take_first
(checked,
uncheckedโ ,
mut,
mut_checked,
mut_uncheckedโ )
โ
-
- lsplit (mut)
- rsplit (mut)
- msplit_left (mut)
- msplit_right (mut)
-
- chunks_exact (mut)
- chunk (mut)
Implementationsยง
Sourceยงimpl<T> Slice<T>
ยงcore::slice namespaced methods.
impl<T> Slice<T>
ยงcore::slice namespaced methods.
Sourcepub fn clone(dst: &mut [T], src: &[T])where
T: Clone,
pub fn clone(dst: &mut [T], src: &[T])where
T: Clone,
Clones all elements from src into dst.
ยงPanics
Panics if src and dst slices have different lengths.
See core::slice::clone_from_slice.
Sourcepub const fn copy(dst: &mut [T], src: &[T])where
T: Copy,
pub const fn copy(dst: &mut [T], src: &[T])where
T: Copy,
Copies all elements from src into dst using a memcpy.
ยงPanics
Panics if src and dst slices have different lengths.
See core::slice::copy_from_slice.
Sourcepub const fn get(slice: &[T], index: usize) -> Option<&T> โ
pub const fn get(slice: &[T], index: usize) -> Option<&T> โ
Returns a shared reference to the element at index, or None if out of bounds.
This is a stable const analogue just for single-element checked access.
See core::slice::get.
Sourcepub const fn get_mut(slice: &mut [T], index: usize) -> Option<&mut T> โ
pub const fn get_mut(slice: &mut [T], index: usize) -> Option<&mut T> โ
Returns an exclusive reference to the element at index, or None if out of bounds.
This is a stable const analogue just for single-element checked access.
See core::slice::get_mut.
Sourcepub const fn from_ref(s: &T) -> &[T] โ
pub const fn from_ref(s: &T) -> &[T] โ
Converts a reference to T into a slice of length 1 (without copying).
See core::slice::from_ref.
Sourcepub const fn from_mut(s: &mut T) -> &mut [T] โ
pub const fn from_mut(s: &mut T) -> &mut [T] โ
Converts a reference to T into a slice of length 1 (without copying).
See core::slice::from_mut.
Sourcepub const unsafe fn from_raw_parts<'a>(data: *const T, len: usize) -> &'a [T] โ
Available on unsafeยทยท only.
pub const unsafe fn from_raw_parts<'a>(data: *const T, len: usize) -> &'a [T] โ
unsafeยทยท only.Forms a shared slice from a pointer and a length.
ยงSafety
See core::slice::from_raw_parts
See also Ptr::slice_from_raw_parts.
Sourcepub const unsafe fn from_raw_parts_mut<'a>(
data: *mut T,
len: usize,
) -> &'a mut [T] โ
Available on unsafeยทยท only.
pub const unsafe fn from_raw_parts_mut<'a>( data: *mut T, len: usize, ) -> &'a mut [T] โ
unsafeยทยท only.Forms an exclusive slice from a pointer and a length.
ยงSafety
See core::slice::from_raw_parts_mut.
See also Ptr::slice_from_raw_parts_mut.
Sourceยงimpl<T> Slice<T>
ยงrange* API methods for subslicing.
impl<T> Slice<T>
ยงrange* API methods for subslicing.
Sourcepub const fn range_to(slice: &[T], end: usize) -> &[T] โ
pub const fn range_to(slice: &[T], end: usize) -> &[T] โ
Returns a subslice up to the given end index.
Equivalent to &slice[..end].
ยงPanics
Panics if end > slice.len().
Sourcepub const fn range_to_checked(slice: &[T], end: usize) -> Option<&[T]> โ
pub const fn range_to_checked(slice: &[T], end: usize) -> Option<&[T]> โ
Returns a subslice up to the given end index.
Equivalent to &slice[..end].
Returns None if end > slice.len().
Sourcepub const unsafe fn range_to_unchecked(slice: &[T], end: usize) -> &[T] โ
Available on crate feature unsafe_slice only.
pub const unsafe fn range_to_unchecked(slice: &[T], end: usize) -> &[T] โ
unsafe_slice only.Returns a subslice up to the given end index.
Equivalent to &slice[..end].
ยงSafety
Results in undefined behavior if end > slice.len().
Sourcepub const fn range_to_mut(slice: &mut [T], end: usize) -> &mut [T] โ
pub const fn range_to_mut(slice: &mut [T], end: usize) -> &mut [T] โ
Returns an exclusive subslice up to the given end index.
Equivalent to &mut slice[..end].
ยงPanics
Panics if end > slice.len().
Sourcepub const fn range_to_mut_checked(
slice: &mut [T],
end: usize,
) -> Option<&mut [T]> โ
pub const fn range_to_mut_checked( slice: &mut [T], end: usize, ) -> Option<&mut [T]> โ
Returns an exclusive subslice up to the given end index.
Equivalent to &mut slice[..end].
Returns None if end > slice.len().
Sourcepub const unsafe fn range_to_mut_unchecked(
slice: &mut [T],
end: usize,
) -> &mut [T] โ
Available on crate feature unsafe_slice only.
pub const unsafe fn range_to_mut_unchecked( slice: &mut [T], end: usize, ) -> &mut [T] โ
unsafe_slice only.Returns an exclusive subslice up to the given end index.
Equivalent to &mut slice[..end].
ยงSafety
Results in undefined behavior if end > slice.len().
Sourcepub const fn range_to_inclusive(slice: &[T], end: usize) -> &[T] โ
pub const fn range_to_inclusive(slice: &[T], end: usize) -> &[T] โ
Returns a subslice up to and including the given end index.
Equivalent to &slice[..=end].
ยงPanics
Panics if end >= slice.len().
Sourcepub const fn range_to_inclusive_checked(slice: &[T], end: usize) -> Option<&[T]> โ
pub const fn range_to_inclusive_checked(slice: &[T], end: usize) -> Option<&[T]> โ
Returns a subslice up to and including the given end index.
Equivalent to &slice[..=end].
Returns None if end >= slice.len().
Sourcepub const unsafe fn range_to_inclusive_unchecked(
slice: &[T],
end: usize,
) -> &[T] โ
Available on crate feature unsafe_slice only.
pub const unsafe fn range_to_inclusive_unchecked( slice: &[T], end: usize, ) -> &[T] โ
unsafe_slice only.Returns an inclusive subslice up to the given end index.
Equivalent to &slice[..=end].
ยงSafety
Results in undefined behavior if end >= slice.len().
Sourcepub const fn range_to_inclusive_mut(slice: &mut [T], end: usize) -> &mut [T] โ
pub const fn range_to_inclusive_mut(slice: &mut [T], end: usize) -> &mut [T] โ
Returns an exclusive subslice up to and including the given end index.
Equivalent to &slice[..=end].
ยงPanics
Panics if end >= slice.len().
Sourcepub const fn range_to_inclusive_mut_checked(
slice: &mut [T],
end: usize,
) -> Option<&mut [T]> โ
pub const fn range_to_inclusive_mut_checked( slice: &mut [T], end: usize, ) -> Option<&mut [T]> โ
Returns an exclusive subslice up to and including the given end index.
Equivalent to &slice[..=end].
Returns None if end >= slice.len().
Sourcepub const unsafe fn range_to_inclusive_mut_unchecked(
slice: &mut [T],
end: usize,
) -> &mut [T] โ
Available on crate feature unsafe_slice only.
pub const unsafe fn range_to_inclusive_mut_unchecked( slice: &mut [T], end: usize, ) -> &mut [T] โ
unsafe_slice only.Returns an inclusive subslice up to the given end index.
Equivalent to &slice[..=end].
ยงSafety
Results in undefined behavior if end >= slice.len().
Sourcepub const fn range_from(slice: &[T], start: usize) -> &[T] โ
pub const fn range_from(slice: &[T], start: usize) -> &[T] โ
Returns a subslice starting from the given start index.
Equivalent to &slice[start..].
ยงPanics
Panics if start > slice.len().
Sourcepub const fn range_from_checked(slice: &[T], start: usize) -> Option<&[T]> โ
pub const fn range_from_checked(slice: &[T], start: usize) -> Option<&[T]> โ
Returns a subslice starting from the given start index.
Equivalent to &slice[start..].
Returns None if start > slice.len().
Sourcepub const unsafe fn range_from_unchecked(slice: &[T], start: usize) -> &[T] โ
Available on crate feature unsafe_slice only.
pub const unsafe fn range_from_unchecked(slice: &[T], start: usize) -> &[T] โ
unsafe_slice only.Returns an exclusive subslice starting from the given start index.
Equivalent to &slice[start..].
ยงSafety
Results in undefined behavior if start > slice.len().
Sourcepub const fn range_from_mut(slice: &mut [T], start: usize) -> &mut [T] โ
pub const fn range_from_mut(slice: &mut [T], start: usize) -> &mut [T] โ
Returns an exclusive subslice starting from the given start index.
Equivalent to &mut slice[start..].
ยงPanics
Panics if start > slice.len().
Sourcepub const fn range_from_mut_checked(
slice: &mut [T],
start: usize,
) -> Option<&mut [T]> โ
pub const fn range_from_mut_checked( slice: &mut [T], start: usize, ) -> Option<&mut [T]> โ
Returns an exclusive subslice starting from the given start index.
Equivalent to &mut slice[start..].
Returns None if start > slice.len().
Sourcepub const unsafe fn range_from_mut_unchecked(
slice: &mut [T],
start: usize,
) -> &mut [T] โ
Available on crate feature unsafe_slice only.
pub const unsafe fn range_from_mut_unchecked( slice: &mut [T], start: usize, ) -> &mut [T] โ
unsafe_slice only.Returns an exclusive subslice starting from the given start index.
Equivalent to &mut slice[start..].
ยงSafety
Results in undefined behavior if start > slice.len().
Sourcepub const fn range(slice: &[T], start: usize, end: usize) -> &[T] โ
pub const fn range(slice: &[T], start: usize, end: usize) -> &[T] โ
Returns a subslice from start (inclusive) to end (exclusive).
Equivalent to &slice[start..end].
ยงPanics
Panics if start > end or end > slice.len().
Sourcepub const fn range_checked(
slice: &[T],
start: usize,
end: usize,
) -> Option<&[T]> โ
pub const fn range_checked( slice: &[T], start: usize, end: usize, ) -> Option<&[T]> โ
Returns a subslice from start (inclusive) to end (exclusive).
Equivalent to &slice[start..end].
Returns None if start > end or end > slice.len().
ยงFeatures
This method makes use of of the unsafe_slice feature is enabled.
Sourcepub const unsafe fn range_unchecked(
slice: &[T],
start: usize,
end: usize,
) -> &[T] โ
Available on crate feature unsafe_slice only.
pub const unsafe fn range_unchecked( slice: &[T], start: usize, end: usize, ) -> &[T] โ
unsafe_slice only.Returns a subslice from start (inclusive) to end (exclusive).
Equivalent to &slice[start..end].
ยงSafety
Results in undefined behavior if start > slice.len() or end > slice.len().
Sourcepub const fn range_mut(slice: &mut [T], start: usize, end: usize) -> &mut [T] โ
pub const fn range_mut(slice: &mut [T], start: usize, end: usize) -> &mut [T] โ
Returns an exclusive subslice from start (inclusive) to end (exclusive).
Equivalent to &mut slice[start..end].
ยงPanics
Panics if start > end or end > slice.len().
Sourcepub const fn range_mut_checked(
slice: &mut [T],
start: usize,
end: usize,
) -> Option<&mut [T]> โ
pub const fn range_mut_checked( slice: &mut [T], start: usize, end: usize, ) -> Option<&mut [T]> โ
Returns an exclusive subslice from start (inclusive) to end (exclusive).
Equivalent to &mut slice[start..end].
Returns None if start > end or end > slice.len().
ยงFeatures
This method makes use of of the unsafe_slice feature is enabled.
Sourcepub const unsafe fn range_mut_unchecked(
slice: &mut [T],
start: usize,
end: usize,
) -> &mut [T] โ
Available on crate feature unsafe_slice only.
pub const unsafe fn range_mut_unchecked( slice: &mut [T], start: usize, end: usize, ) -> &mut [T] โ
unsafe_slice only.Returns an exclusive subslice from start (inclusive) to end (exclusive).
Equivalent to &mut slice[start..end].
ยงSafety
Results in undefined behavior if start > end or end > slice.len().
Sourcepub const fn range_inclusive(slice: &[T], start: usize, end: usize) -> &[T] โ
pub const fn range_inclusive(slice: &[T], start: usize, end: usize) -> &[T] โ
Returns a subslice from start (inclusive) to end (inclusive).
Equivalent to &slice[start..=end].
ยงPanics
Panics if start > end or end >= slice.len().
Sourcepub const fn range_inclusive_checked(
slice: &[T],
start: usize,
end: usize,
) -> Option<&[T]> โ
pub const fn range_inclusive_checked( slice: &[T], start: usize, end: usize, ) -> Option<&[T]> โ
Returns a subslice from start (inclusive) to end (inclusive).
Equivalent to &slice[start..=end].
Returns None if start > end or end >= slice.len().
ยงFeatures
This method makes use of the unsafe_slice feature if enabled.
Sourcepub const unsafe fn range_inclusive_unchecked(
slice: &[T],
start: usize,
end: usize,
) -> &[T] โ
Available on crate feature unsafe_slice only.
pub const unsafe fn range_inclusive_unchecked( slice: &[T], start: usize, end: usize, ) -> &[T] โ
unsafe_slice only.Returns a subslice from start (inclusive) to end (inclusive).
Equivalent to &slice[start..=end].
ยงSafety
Results in undefined behavior if start > slice.len() or end >= slice.len().
Sourcepub const fn range_inclusive_mut(
slice: &mut [T],
start: usize,
end: usize,
) -> &mut [T] โ
pub const fn range_inclusive_mut( slice: &mut [T], start: usize, end: usize, ) -> &mut [T] โ
Returns a subslice from start (inclusive) to end (inclusive).
Equivalent to &mut slice[start..=end].
ยงPanics
Panics if start > end or end >= slice.len().
Sourcepub const fn range_inclusive_mut_checked(
slice: &mut [T],
start: usize,
end: usize,
) -> Option<&mut [T]> โ
pub const fn range_inclusive_mut_checked( slice: &mut [T], start: usize, end: usize, ) -> Option<&mut [T]> โ
Returns a subslice from start (inclusive) to end (inclusive).
Equivalent to &mut slice[start..=end].
Returns None if start > end or end >= slice.len().
ยงFeatures
This method makes use of the unsafe_slice feature if enabled.
Sourcepub const unsafe fn range_inclusive_mut_unchecked(
slice: &mut [T],
start: usize,
end: usize,
) -> &mut [T] โ
Available on crate feature unsafe_slice only.
pub const unsafe fn range_inclusive_mut_unchecked( slice: &mut [T], start: usize, end: usize, ) -> &mut [T] โ
unsafe_slice only.Returns a subslice from start (inclusive) to end (inclusive).
Equivalent to &mut slice[start..=end].
ยงSafety
Results in undefined behavior if start > slice.len() or end >= slice.len().
Sourceยงimpl<T> Slice<T>
ยงtake* API methods for subslicing.
impl<T> Slice<T>
ยงtake* API methods for subslicing.
Sourcepub const fn take_first(slice: &[T], n: usize) -> &[T] โ
pub const fn take_first(slice: &[T], n: usize) -> &[T] โ
Returns the first n elements of the slice.
Equivalent to &slice[..n].
ยงPanics
Panics if n > slice.len().
Sourcepub const fn take_first_checked(slice: &[T], n: usize) -> Option<&[T]> โ
pub const fn take_first_checked(slice: &[T], n: usize) -> Option<&[T]> โ
Returns the first n elements of the slice.
Equivalent to &slice[..n].
Returns None if n > slice.len().
Sourcepub const unsafe fn take_first_unchecked(slice: &[T], n: usize) -> &[T] โ
Available on crate feature unsafe_slice only.
pub const unsafe fn take_first_unchecked(slice: &[T], n: usize) -> &[T] โ
unsafe_slice only.Returns the first n elements of the slice.
Equivalent to &slice[..n].
ยงSafety
Results in undefined behavior if n > slice.len().
Sourcepub const fn take_first_mut(slice: &mut [T], n: usize) -> &mut [T] โ
pub const fn take_first_mut(slice: &mut [T], n: usize) -> &mut [T] โ
Returns the first n elements of the exclusive slice.
Equivalent to &mut slice[..n].
ยงPanics
Panics if n > slice.len().
Sourcepub const fn take_first_mut_checked(
slice: &mut [T],
n: usize,
) -> Option<&mut [T]> โ
pub const fn take_first_mut_checked( slice: &mut [T], n: usize, ) -> Option<&mut [T]> โ
Returns the first n elements of the exclusive slice.
Equivalent to &mut slice[..n].
Returns None if n > slice.len().
Sourcepub const unsafe fn take_first_mut_unchecked(
slice: &mut [T],
n: usize,
) -> &mut [T] โ
Available on crate feature unsafe_slice only.
pub const unsafe fn take_first_mut_unchecked( slice: &mut [T], n: usize, ) -> &mut [T] โ
unsafe_slice only.Returns the first n elements of the exclusive slice.
Equivalent to &mut slice[..n].
ยงSafety
Results in undefined behavior if n > slice.len().
Sourcepub const fn take_last(slice: &[T], n: usize) -> &[T] โ
pub const fn take_last(slice: &[T], n: usize) -> &[T] โ
Returns the last n elements of the slice.
Equivalent to &slice[slice.len() - n..].
ยงPanics
Panics if n > slice.len().
Sourcepub const fn take_last_checked(slice: &[T], n: usize) -> Option<&[T]> โ
pub const fn take_last_checked(slice: &[T], n: usize) -> Option<&[T]> โ
Returns the last n elements of the slice.
Equivalent to &slice[slice.len() - n..].
Returns None if n > slice.len().
ยงFeatures
This method makes use of of the unsafe_slice feature is enabled.
Sourcepub const unsafe fn take_last_unchecked(slice: &[T], n: usize) -> &[T] โ
Available on crate feature unsafe_slice only.
pub const unsafe fn take_last_unchecked(slice: &[T], n: usize) -> &[T] โ
unsafe_slice only.Returns the last n elements of the slice.
Equivalent to &slice[slice.len() - n..].
ยงSafety
Results in undefined behavior if n > slice.len().
Sourcepub const fn take_last_mut(slice: &mut [T], n: usize) -> &mut [T] โ
pub const fn take_last_mut(slice: &mut [T], n: usize) -> &mut [T] โ
Returns the last n elements of the exclusive slice.
Equivalent to &mut slice[slice.len() - n..].
ยงPanics
Panics if n > slice.len().
Sourcepub const fn take_last_mut_checked(
slice: &mut [T],
n: usize,
) -> Option<&mut [T]> โ
pub const fn take_last_mut_checked( slice: &mut [T], n: usize, ) -> Option<&mut [T]> โ
Returns the last n elements of the exclusive slice.
Equivalent to &mut slice[slice.len() - n..].
Returns None if n > slice.len().
ยงFeatures
This method makes use of of the unsafe_slice feature is enabled.
Sourcepub const unsafe fn take_last_mut_unchecked(
slice: &mut [T],
n: usize,
) -> &mut [T] โ
Available on crate feature unsafe_slice only.
pub const unsafe fn take_last_mut_unchecked( slice: &mut [T], n: usize, ) -> &mut [T] โ
unsafe_slice only.Returns the last n elements of the exclusive slice.
Equivalent to &mut slice[slice.len() - n..].
ยงSafety
Results in undefined behavior if n > slice.len().
Sourcepub const fn take_omit_last(slice: &[T], n: usize) -> &[T] โ
pub const fn take_omit_last(slice: &[T], n: usize) -> &[T] โ
Returns the slice omitting the last n elements.
Equivalent to &slice[..slice.len() - n].
ยงPanics
Panics if n > slice.len().
Sourcepub const fn take_omit_last_checked(slice: &[T], n: usize) -> Option<&[T]> โ
pub const fn take_omit_last_checked(slice: &[T], n: usize) -> Option<&[T]> โ
Returns the slice omitting the last n elements.
Equivalent to &slice[..slice.len() - n].
Returns None if n > slice.len().
ยงFeatures
This method makes use of of the unsafe_slice feature is enabled.
Sourcepub const fn take_omit_last_unchecked(slice: &[T], n: usize) -> &[T] โ
Available on crate feature unsafe_slice only.
pub const fn take_omit_last_unchecked(slice: &[T], n: usize) -> &[T] โ
unsafe_slice only.Returns the slice omitting the last n elements.
Equivalent to &slice[..slice.len() - n].
ยงSafety
Results in undefined behavior if n > slice.len().
Sourcepub const fn take_omit_last_mut(slice: &mut [T], n: usize) -> &mut [T] โ
pub const fn take_omit_last_mut(slice: &mut [T], n: usize) -> &mut [T] โ
Returns the exclusive slice omitting the last n elements.
Equivalent to &mut slice[..slice.len() - n].
ยงPanics
Panics if n > slice.len().
Sourcepub const fn take_omit_last_mut_checked(
slice: &mut [T],
n: usize,
) -> Option<&mut [T]> โ
pub const fn take_omit_last_mut_checked( slice: &mut [T], n: usize, ) -> Option<&mut [T]> โ
Returns the exclusive slice omitting the last n elements.
Equivalent to &mut slice[..slice.len() - n].
Returns None if n > slice.len().
ยงFeatures
This method makes use of of the unsafe_slice feature is enabled.
Sourceยงimpl<T> Slice<T>
ยง*split* API methods for subslicing.
impl<T> Slice<T>
ยง*split* API methods for subslicing.
Sourcepub const fn lsplit(slice: &[T], len: usize) -> &[T] โ
pub const fn lsplit(slice: &[T], len: usize) -> &[T] โ
Returns the leftmost sub-slice with the given maximum len.
If len > self.len() it returns the full slice.
ยงExamples
let v = [1, 2, 3, 4, 5, 6];
assert_eq!(Slice::lsplit(&v, 3), &[1, 2, 3]);
assert_eq!(Slice::lsplit(&v, 0), &[] as &[i32]);
assert_eq!(Slice::lsplit(&v, 10), &[1, 2, 3, 4, 5, 6]);Sourcepub const fn lsplit_mut(slice: &mut [T], len: usize) -> &mut [T] โ
pub const fn lsplit_mut(slice: &mut [T], len: usize) -> &mut [T] โ
Returns the leftmost exclusive sub-slice with the given maximum len.
If left_len > slice.len() it returns the full slice.
ยงExamples
let mut v = [1, 2, 3, 4, 5, 6];
assert_eq!(Slice::lsplit_mut(&mut v, 3), &mut [1, 2, 3]);
assert_eq!(Slice::lsplit_mut(&mut v, 0), &mut [] as &mut [i32]);
assert_eq!(Slice::lsplit_mut(&mut v, 10), &mut [1, 2, 3, 4, 5, 6]);See also Slice::lsplit_mut.
Sourcepub const fn rsplit(slice: &[T], len: usize) -> &[T] โ
pub const fn rsplit(slice: &[T], len: usize) -> &[T] โ
Returns the rightmost sub-slice with the given maximum len.
If left_len > slice.len() it returns the full slice.
ยงExamples
let v = [1, 2, 3, 4, 5, 6];
assert_eq!(Slice::rsplit(&v, 3), &[4, 5, 6]);
assert_eq!(Slice::rsplit(&v, 0), &[] as &[i32]);
assert_eq!(Slice::rsplit(&v, 10), &[1, 2, 3, 4, 5, 6]);Sourcepub const fn rsplit_mut(slice: &mut [T], len: usize) -> &mut [T] โ
pub const fn rsplit_mut(slice: &mut [T], len: usize) -> &mut [T] โ
Returns the rightmost exclusive sub-slice with the given maximum len.
If left_len > slice.len() it returns the full slice.
ยงExamples
let mut v = [1, 2, 3, 4, 5, 6];
assert_eq!(Slice::rsplit_mut(&mut v, 3), &mut [4, 5, 6]);
assert_eq!(Slice::rsplit_mut(&mut v, 0), &mut [] as &mut [i32]);
assert_eq!(Slice::rsplit_mut(&mut v, 10), &mut [1, 2, 3, 4, 5, 6]);See also Slice::lsplit_mut.
Sourcepub const fn msplit_left(slice: &[T], len: usize) -> &[T] โ
pub const fn msplit_left(slice: &[T], len: usize) -> &[T] โ
Returns the middle sub-slice with the given maximum len and a left bias.
In case of a non-perfect middle split, it will have one element more on the left.
If len > slice.len() returns the full slice.
ยงExamples
let v = [1, 2, 3, 4, 5, 6];
assert_eq!(Slice::msplit_left(&v, 0), &[] as &[i32]);
assert_eq!(Slice::msplit_left(&v, 1), &[3]);
assert_eq!(Slice::msplit_left(&v, 2), &[3, 4]);
assert_eq!(Slice::msplit_left(&v, 3), &[2, 3, 4]);
assert_eq!(Slice::msplit_left(&v, 4), &[2, 3, 4, 5]);
assert_eq!(Slice::msplit_left(&v, 5), &[1, 2, 3, 4, 5]);
assert_eq!(Slice::msplit_left(&v, 10), &[1, 2, 3, 4, 5, 6]);See also Slice::msplit_right.
Sourcepub const fn msplit_left_mut(slice: &mut [T], len: usize) -> &mut [T] โ
pub const fn msplit_left_mut(slice: &mut [T], len: usize) -> &mut [T] โ
Returns the middle exclusive sub-slice with the given maximum len and a
left bias.
In case of a non-perfect middle split, it will have one element more on the left.
If len > slice.len() returns the full slice.
ยงExamples
let mut v = [1, 2, 3, 4, 5, 6];
assert_eq!(Slice::msplit_left_mut(&mut v, 0), &mut [] as &mut [i32]);
assert_eq!(Slice::msplit_left_mut(&mut v, 1), &mut [3]);
assert_eq!(Slice::msplit_left_mut(&mut v, 2), &mut [3, 4]);
assert_eq!(Slice::msplit_left_mut(&mut v, 3), &mut [2, 3, 4]);
assert_eq!(Slice::msplit_left_mut(&mut v, 4), &mut [2, 3, 4, 5]);
assert_eq!(Slice::msplit_left_mut(&mut v, 5), &mut [1, 2, 3, 4, 5]);
assert_eq!(Slice::msplit_left_mut(&mut v, 10), &mut [1, 2, 3, 4, 5, 6]);See also Slice::msplit_right_mut.
Sourcepub const fn msplit_right(slice: &[T], len: usize) -> &[T] โ
pub const fn msplit_right(slice: &[T], len: usize) -> &[T] โ
Returns the middle sub-slice with the given maximum len and a right bias.
In case of a non-perfect middle split, it will have one element more on the right.
If len > slice.len() returns the full slice.
ยงExamples
let v = [1, 2, 3, 4, 5, 6];
assert_eq!(Slice::msplit_right(&v, 0), &[] as &[i32]);
assert_eq!(Slice::msplit_right(&v, 1), &[4]);
assert_eq!(Slice::msplit_right(&v, 2), &[3, 4]);
assert_eq!(Slice::msplit_right(&v, 3), &[3, 4, 5]);
assert_eq!(Slice::msplit_right(&v, 4), &[2, 3, 4, 5]);
assert_eq!(Slice::msplit_right(&v, 5), &[2, 3, 4, 5, 6]);
assert_eq!(Slice::msplit_right(&v, 10), &[1, 2, 3, 4, 5, 6]);See also Slice::msplit_left.
Sourcepub const fn msplit_right_mut(slice: &mut [T], len: usize) -> &mut [T] โ
pub const fn msplit_right_mut(slice: &mut [T], len: usize) -> &mut [T] โ
Returns the middle exclusive sub-slice with the given maximum len and a
right bias.
In case of a non-perfect middle split, it will have one element more on the right.
If len > slice.len() returns the full slice.
ยงExamples
let mut v = [1, 2, 3, 4, 5, 6];
assert_eq!(Slice::msplit_right_mut(&mut v, 0), &mut [] as &mut[i32]);
assert_eq!(Slice::msplit_right_mut(&mut v, 1), &mut [4]);
assert_eq!(Slice::msplit_right_mut(&mut v, 2), &mut [3, 4]);
assert_eq!(Slice::msplit_right_mut(&mut v, 3), &mut [3, 4, 5]);
assert_eq!(Slice::msplit_right_mut(&mut v, 4), &mut [2, 3, 4, 5]);
assert_eq!(Slice::msplit_right_mut(&mut v, 5), &mut [2, 3, 4, 5, 6]);
assert_eq!(Slice::msplit_right_mut(&mut v, 10), &mut [1, 2, 3, 4, 5, 6]);See also Slice::msplit_left_mut.
Sourceยงimpl<T> Slice<T>
ยง*chunk* API methods for subslicing.
impl<T> Slice<T>
ยง*chunk* API methods for subslicing.
Sourcepub const fn chunks_exact<const N: usize>(slice: &[T]) -> (usize, &[T]) โ
pub const fn chunks_exact<const N: usize>(slice: &[T]) -> (usize, &[T]) โ
Returns the number of complete N-sized chunks in slice and the trailing remainder.
This is the fixed-step primitive for const-friendly traversal.
ยงExamples
let bytes = b"abcdefg";
let (count, rem) = Slice::chunks_exact::<3>(bytes);
assert_eq!(count, 2); // "abc", "def"
assert_eq!(rem, b"g");Sourcepub const fn chunks_exact_mut<const N: usize>(
slice: &mut [T],
) -> (usize, &mut [T]) โ
pub const fn chunks_exact_mut<const N: usize>( slice: &mut [T], ) -> (usize, &mut [T]) โ
Mutable counterpart to chunks_exact.
Returns the number of complete N-sized chunks and a mutable remainder slice.
The caller performs any iteration or stepping logic.
Sourcepub const fn chunk<const N: usize>(slice: &[T], idx: usize) -> Option<&[T]> โ
pub const fn chunk<const N: usize>(slice: &[T], idx: usize) -> Option<&[T]> โ
Returns the idx-th complete chunk of size N, or None if incomplete.
This offers direct, index-based access to fixed-width records. Bounds are checked; no iteration policy is imposed.
ยงExamples
let b = b"abcdefgh";
assert_eq!(Slice::chunk::<3>(b, 0), Some(&b"abc"[..]));
assert_eq!(Slice::chunk::<3>(b, 1), Some(&b"def"[..]));
assert_eq!(Slice::chunk::<3>(b, 2), None); // only "gh" remains, not enough for a full chunkSourceยงimpl Slice<u8>
ยงMethods for byte slices.
impl Slice<u8>
ยงMethods for byte slices.
Sourcepub const fn copy_into(dst: &mut [u8], dst_pos: usize, src: &[u8]) -> usize
pub const fn copy_into(dst: &mut [u8], dst_pos: usize, src: &[u8]) -> usize
Copies bytes from src into dst starting at dst_pos, up to the remaining capacity.
Returns the number of bytes copied (possibly less than src.len() if truncated).
ยงExamples
let mut buf = [0u8; 4];
assert_eq!(Slice::copy_into(&mut buf, 1, b"abc"), 3);
assert_eq!(&buf, b"\0abc");Sourcepub const fn copy_str_into(dst: &mut [u8], dst_pos: usize, src: &str) -> usize
pub const fn copy_str_into(dst: &mut [u8], dst_pos: usize, src: &str) -> usize
Copies UTF-8 bytes from src into dst starting at dst_pos,
truncating only at valid UTF-8 character boundaries.
Returns the number of bytes copied (never splits a character).
ยงExamples
// 'o' (1 byte) would fit, but 'รธ' (2 bytes) must be dropped
let mut buf = [0u8; 5];
assert_eq!(Slice::copy_str_into(&mut buf, 0, "hellรธ"), 4);
assert_eq!(&buf[..4], b"hell");Sourcepub const fn copy_utf8_into(dst: &mut [u8], dst_pos: usize, src: &[u8]) -> usize
pub const fn copy_utf8_into(dst: &mut [u8], dst_pos: usize, src: &[u8]) -> usize
Copies bytes from a UTF-8โencoded slice into dst starting at dst_pos,
truncating only at valid UTF-8 character boundaries.
Returns the number of bytes copied (never splits a UTF-8 sequence).
ยงExamples
// 'o' (1 byte) would fit, but 'รธ' (2 bytes) must be dropped
let mut buf = [0u8; 5];
assert_eq!(Slice::copy_utf8_into(&mut buf, 0, "hellรธ".as_bytes()), 4);
assert_eq!(&buf[..4], b"hell");Sourcepub const fn copy_array<const N: usize>(dst: &mut [u8; N], src: &[u8; N])
pub const fn copy_array<const N: usize>(dst: &mut [u8; N], src: &[u8; N])
Copies the src byte array into dst in compile-time.
ยงFeatures
- Uses
Ptr::copy_nonoverlappingwhen unsafe operations are allowed.
Sourcepub const fn copy_array_at<const LEN: usize>(
dst: &mut [u8; LEN],
src: &[u8],
offset: usize,
)
pub const fn copy_array_at<const LEN: usize>( dst: &mut [u8; LEN], src: &[u8], offset: usize, )
Sourcepub const fn copied_array_at<const LEN: usize>(
base: [u8; LEN],
src: &[u8],
offset: usize,
) -> [u8; LEN] โ
pub const fn copied_array_at<const LEN: usize>( base: [u8; LEN], src: &[u8], offset: usize, ) -> [u8; LEN] โ
A convenience wrapper over Slice::copy_array_at.
Sourcepub const fn replace_leading(slice: &mut [u8], old: u8, new: u8)
pub const fn replace_leading(slice: &mut [u8], old: u8, new: u8)
Replaces the old leading byte with a new byte.
Sourcepub const fn trim_leading(slice: &[u8], byte: u8) -> &[u8] โ
pub const fn trim_leading(slice: &[u8], byte: u8) -> &[u8] โ
Returns a subslice without the given leading bytes.
ยงExamples
assert_eq!(Slice::trim_leading(b"000123", b'0'), b"123");Sourcepub const fn trim_leading_keep(slice: &[u8], byte: u8, keep: usize) -> &[u8] โ
pub const fn trim_leading_keep(slice: &[u8], byte: u8, keep: usize) -> &[u8] โ
Returns a subslice without the given leading bytes, except of keep number of them.
ยงExamples
assert_eq!(Slice::trim_leading_keep(b"000123", b'0', 0), b"123");
assert_eq!(Slice::trim_leading_keep(b"000123", b'0', 2), b"00123");
assert_eq!(Slice::trim_leading_keep(b"000123", b'0', 10), b"000123");Sourcepub const fn trim_leading_min_len(
slice: &[u8],
byte: u8,
min_len: usize,
) -> &[u8] โ
pub const fn trim_leading_min_len( slice: &[u8], byte: u8, min_len: usize, ) -> &[u8] โ
Returns a subslice without the given leading bytes,
ensuring the result has at least min_len bytes (if possible).
ยงExamples
assert_eq!(Slice::trim_leading_min_len(b"000000", b'0', 1), b"0");
assert_eq!(Slice::trim_leading_min_len(b"000123", b'0', 0), b"123");
assert_eq!(Slice::trim_leading_min_len(b"000123", b'0', 4), b"0123");
assert_eq!(Slice::trim_leading_min_len(b"000123", b'0', 10), b"000123");Sourcepub const fn trim_trailing(slice: &[u8], byte: u8) -> &[u8] โ
pub const fn trim_trailing(slice: &[u8], byte: u8) -> &[u8] โ
Returns a subslice without the given trailing bytes.
ยงExamples
assert_eq!(Slice::trim_trailing(b"123000", b'0'), b"123");Sourcepub const fn trim_trailing_keep(slice: &[u8], byte: u8, keep: usize) -> &[u8] โ
pub const fn trim_trailing_keep(slice: &[u8], byte: u8, keep: usize) -> &[u8] โ
Returns a subslice without the given trailing bytes, except of keep number of them.
ยงExamples
assert_eq!(Slice::trim_trailing_keep(b"123000", b'0', 0), b"123");
assert_eq!(Slice::trim_trailing_keep(b"123000", b'0', 2), b"12300");
assert_eq!(Slice::trim_trailing_keep(b"123000", b'0', 10), b"123000");Sourcepub const fn trim_trailing_min_len(
slice: &[u8],
byte: u8,
min_len: usize,
) -> &[u8] โ
pub const fn trim_trailing_min_len( slice: &[u8], byte: u8, min_len: usize, ) -> &[u8] โ
Returns a subslice without the given trailing bytes,
ensuring the result has at least min_len bytes (if possible).
ยงExamples
assert_eq!(Slice::trim_trailing_min_len(b"000000", b'0', 1), b"0");
assert_eq!(Slice::trim_trailing_min_len(b"123000", b'0', 0), b"123");
assert_eq!(Slice::trim_trailing_min_len(b"123000", b'0', 4), b"1230");
assert_eq!(Slice::trim_trailing_min_len(b"123000", b'0', 10), b"123000");Sourcepub const fn trim_edges(slice: &[u8], byte: u8) -> &[u8] โ
pub const fn trim_edges(slice: &[u8], byte: u8) -> &[u8] โ
Returns a subslice without the given leading and trailing bytes.
ยงExamples
assert_eq!(Slice::trim_edges(b"000123000", b'0'), b"123");Sourcepub const fn trim_edges_keep(slice: &[u8], byte: u8, keep: usize) -> &[u8] โ
pub const fn trim_edges_keep(slice: &[u8], byte: u8, keep: usize) -> &[u8] โ
Returns a subslice without the given leading and trailing bytes,
except for keep number of them on each side.
ยงExamples
assert_eq!(Slice::trim_edges_keep(b"000123000", b'0', 0), b"123");
assert_eq!(Slice::trim_edges_keep(b"000123000", b'0', 1), b"01230");
assert_eq!(Slice::trim_edges_keep(b"000123000", b'0', 2), b"0012300");
assert_eq!(Slice::trim_edges_keep(b"000123000", b'0', 10), b"000123000");Sourcepub const fn trim_edges_min_len_left(
slice: &[u8],
byte: u8,
min_len: usize,
) -> &[u8] โ
pub const fn trim_edges_min_len_left( slice: &[u8], byte: u8, min_len: usize, ) -> &[u8] โ
Returns a subslice without the given leading and edges bytes,
ensuring the result has at least min_len bytes (if possible), with a left bias.
ยงExamples
assert_eq!(Slice::trim_edges_min_len_left(b"000123000", b'0', 0), b"123");
assert_eq!(Slice::trim_edges_min_len_left(b"000123000", b'0', 4), b"0123");
assert_eq!(Slice::trim_edges_min_len_left(b"000123000", b'0', 5), b"01230");
assert_eq!(Slice::trim_edges_min_len_left(b"000123000", b'0', 6), b"001230");
assert_eq!(Slice::trim_edges_min_len_left(b"000123000", b'0', 7), b"0012300");
assert_eq!(Slice::trim_edges_min_len_left(b"000123000", b'0', 20), b"000123000");Sourcepub const fn trim_edges_min_len_right(
slice: &[u8],
byte: u8,
min_len: usize,
) -> &[u8] โ
pub const fn trim_edges_min_len_right( slice: &[u8], byte: u8, min_len: usize, ) -> &[u8] โ
Returns a subslice without the given leading and edges bytes,
ensuring the result has at least min_len bytes (if possible), with a right bias.
ยงExamples
assert_eq!(Slice::trim_edges_min_len_right(b"000123000", b'0', 0), b"123");
assert_eq!(Slice::trim_edges_min_len_right(b"000123000", b'0', 4), b"1230");
assert_eq!(Slice::trim_edges_min_len_right(b"000123000", b'0', 5), b"01230");
assert_eq!(Slice::trim_edges_min_len_right(b"000123000", b'0', 6), b"012300");
assert_eq!(Slice::trim_edges_min_len_right(b"000123000", b'0', 7), b"0012300");
assert_eq!(Slice::trim_edges_min_len_right(b"000123000", b'0', 20), b"000123000");Trait Implementationsยง
Auto Trait Implementationsยง
impl<T> Freeze for Slice<T>
impl<T> RefUnwindSafe for Slice<T>where
T: RefUnwindSafe,
impl<T> Send for Slice<T>where
T: Send,
impl<T> Sync for Slice<T>where
T: Sync,
impl<T> Unpin for Slice<T>where
T: Unpin,
impl<T> UnsafeUnpin for Slice<T>
impl<T> UnwindSafe for Slice<T>where
T: UnwindSafe,
Blanket Implementationsยง
Sourceยงimpl<T> AnyExt for T
impl<T> AnyExt for T
Sourceยงfn type_hash_with<H: Hasher>(&self, hasher: H) -> u64
fn type_hash_with<H: Hasher>(&self, hasher: H) -> u64
TypeId of Self using a custom hasher.Sourceยงfn as_any_mut(&mut self) -> &mut dyn Anywhere
Self: Sized,
fn as_any_mut(&mut self) -> &mut dyn Anywhere
Self: Sized,
Sourceยงfn as_any_box(self: Box<Self>) -> Box<dyn Any>where
Self: Sized,
fn as_any_box(self: Box<Self>) -> Box<dyn Any>where
Self: Sized,
alloc only.Sourceยงimpl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Sourceยงfn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Sourceยงimpl<T> ByteSized for T
impl<T> ByteSized for T
Sourceยงconst BYTE_ALIGN: usize = _
const BYTE_ALIGN: usize = _
Sourceยงfn byte_align(&self) -> usize
fn byte_align(&self) -> usize
Sourceยงfn ptr_size_ratio(&self) -> [usize; 2]
fn ptr_size_ratio(&self) -> [usize; 2]
Sourceยงimpl<T> MemExt for Twhere
T: ?Sized,
impl<T> MemExt for Twhere
T: ?Sized,
Sourceยงconst NEEDS_DROP: bool = _
const NEEDS_DROP: bool = _
Sourceยงfn mem_align_of<T>() -> usize
fn mem_align_of<T>() -> usize
Sourceยงfn mem_align_of_val(&self) -> usize
fn mem_align_of_val(&self) -> usize
Sourceยงfn mem_size_of<T>() -> usize
fn mem_size_of<T>() -> usize
Sourceยงfn mem_size_of_val(&self) -> usize
fn mem_size_of_val(&self) -> usize
Sourceยงfn mem_needs_drop(&self) -> bool
fn mem_needs_drop(&self) -> bool
true if dropping values of this type matters. Read moreSourceยงfn mem_forget(self)where
Self: Sized,
fn mem_forget(self)where
Self: Sized,
self without running its destructor. Read moreSourceยงfn mem_replace(&mut self, other: Self) -> Selfwhere
Self: Sized,
fn mem_replace(&mut self, other: Self) -> Selfwhere
Self: Sized,
Sourceยงunsafe fn mem_zeroed<T>() -> T
unsafe fn mem_zeroed<T>() -> T
unsafe_layout only.T represented by the all-zero byte-pattern. Read moreSourceยงunsafe fn mem_transmute_copy<Src, Dst>(src: &Src) -> Dst
unsafe fn mem_transmute_copy<Src, Dst>(src: &Src) -> Dst
unsafe_layout only.T represented by the all-zero byte-pattern. Read moreSourceยงfn mem_as_bytes(&self) -> &[u8] โ
fn mem_as_bytes(&self) -> &[u8] โ
unsafe_slice only.