Skip to main content

Slice

Struct Slice 

Source
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

See also: slice! and SliceExt.

Implementationsยง

Sourceยง

impl<T> Slice<T>

ยงcore::slice namespaced methods.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

pub const unsafe fn from_raw_parts<'a>(data: *const T, len: usize) -> &'a [T] โ“˜

Available on 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.

Source

pub const unsafe fn from_raw_parts_mut<'a>( data: *mut T, len: usize, ) -> &'a mut [T] โ“˜

Available on 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.

Source

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().

Source

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().

Source

pub const unsafe fn range_to_unchecked(slice: &[T], end: usize) -> &[T] โ“˜

Available on crate feature 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().

Source

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().

Source

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().

Source

pub const unsafe fn range_to_mut_unchecked( slice: &mut [T], end: usize, ) -> &mut [T] โ“˜

Available on crate feature 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().

Source

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().

Source

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().

Source

pub const unsafe fn range_to_inclusive_unchecked( slice: &[T], end: usize, ) -> &[T] โ“˜

Available on crate feature 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().

Source

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().

Source

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().

Source

pub const unsafe fn range_to_inclusive_mut_unchecked( slice: &mut [T], end: usize, ) -> &mut [T] โ“˜

Available on crate feature 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().

Source

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().

Source

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().

Source

pub const unsafe fn range_from_unchecked(slice: &[T], start: usize) -> &[T] โ“˜

Available on crate feature 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().

Source

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().

Source

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().

Source

pub const unsafe fn range_from_mut_unchecked( slice: &mut [T], start: usize, ) -> &mut [T] โ“˜

Available on crate feature 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().

Source

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().

Source

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.

Source

pub const unsafe fn range_unchecked( slice: &[T], start: usize, end: usize, ) -> &[T] โ“˜

Available on crate feature 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().

Source

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().

Source

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.

Source

pub const unsafe fn range_mut_unchecked( slice: &mut [T], start: usize, end: usize, ) -> &mut [T] โ“˜

Available on crate feature 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().

Source

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().

Source

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.

Source

pub const unsafe fn range_inclusive_unchecked( slice: &[T], start: usize, end: usize, ) -> &[T] โ“˜

Available on crate feature 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().

Source

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().

Source

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.

Source

pub const unsafe fn range_inclusive_mut_unchecked( slice: &mut [T], start: usize, end: usize, ) -> &mut [T] โ“˜

Available on crate feature 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.

Source

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().

Source

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().

Source

pub const unsafe fn take_first_unchecked(slice: &[T], n: usize) -> &[T] โ“˜

Available on crate feature unsafe_slice only.

Returns the first n elements of the slice.

Equivalent to &slice[..n].

ยงSafety

Results in undefined behavior if n > slice.len().

Source

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().

Source

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().

Source

pub const unsafe fn take_first_mut_unchecked( slice: &mut [T], n: usize, ) -> &mut [T] โ“˜

Available on crate feature 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().

Source

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().

Source

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.

Source

pub const unsafe fn take_last_unchecked(slice: &[T], n: usize) -> &[T] โ“˜

Available on crate feature 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().

Source

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().

Source

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.

Source

pub const unsafe fn take_last_mut_unchecked( slice: &mut [T], n: usize, ) -> &mut [T] โ“˜

Available on crate feature 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().

Source

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().

Source

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.

Source

pub const fn take_omit_last_unchecked(slice: &[T], n: usize) -> &[T] โ“˜

Available on crate feature 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().

Source

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().

Source

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

pub const fn take_omit_last_mut_unchecked(slice: &mut [T], n: usize) -> &mut [T] โ“˜

Available on crate feature unsafe_slice only.

Returns the exclusive slice omitting the last n elements.

Equivalent to &mut slice[..slice.len() - n].

ยงSafety

Results in undefined behavior if n > slice.len().

Sourceยง

impl<T> Slice<T>

ยง*split* API methods for subslicing.

Source

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]);
Source

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.

Source

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]);
Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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");
Source

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.

Source

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 chunk
Source

pub const fn chunk_mut<const N: usize>( slice: &mut [T], idx: usize, ) -> Option<&mut [T]> โ“˜

Mutable counterpart to chunk.

Returns the idx-th complete chunk of size N as a mutable subslice, or None if the chunk is incomplete.

ยงExamples
let mut b = *b"abcdef";
if let Some(ch) = Slice::chunk_mut::<3>(&mut b, 1) {
    ch[0] = b'X';
}
assert_eq!(&b, b"abcXef");
Sourceยง

impl Slice<u8>

ยงMethods for byte slices.

Source

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");
Source

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");
Source

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");
Source

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_nonoverlapping when unsafe operations are allowed.
Source

pub const fn copy_array_at<const LEN: usize>( dst: &mut [u8; LEN], src: &[u8], offset: usize, )

Copies all elements from src into a fixed-size array starting at offset.

ยงPanics

Panics if src.len() + offset > LEN.

ยงFeatures
  • Uses Ptr::copy_nonoverlapping when unsafe operations are allowed.
  • Falls back to safe element-wise copy otherwise.
Source

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.

Source

pub const fn to_array<const LEN: usize>(src: &[u8]) -> [u8; LEN] โ“˜

Copies all elements from src into a new fixed-size array.

ยงFeatures
  • Uses Ptr::copy_nonoverlapping when unsafe operations are allowed
  • Falls back to safe element-wise copy otherwise
ยงPanics

Panics if src.len() != LEN.

Source

pub const fn replace_leading(slice: &mut [u8], old: u8, new: u8)

Replaces the old leading byte with a new byte.

Source

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");
Source

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");
Source

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");
Source

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");
Source

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");
Source

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");
Source

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");
Source

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");
Source

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");
Source

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");
Sourceยง

impl Slice<u8>

Source

pub const fn eq(a: &[u8], b: &[u8]) -> bool

Checks the equality of two slices of primitives in compile-time.

Sourceยง

impl Slice<&[u8]>

Source

pub const fn eq(a: &[&[u8]], b: &[&[u8]]) -> bool

Checks the equality of two slices of slices of primitives in compile-time.

Sourceยง

impl Slice<u16>

Source

pub const fn eq(a: &[u16], b: &[u16]) -> bool

Checks the equality of two slices of primitives in compile-time.

Sourceยง

impl Slice<&[u16]>

Source

pub const fn eq(a: &[&[u16]], b: &[&[u16]]) -> bool

Checks the equality of two slices of slices of primitives in compile-time.

Sourceยง

impl Slice<u32>

Source

pub const fn eq(a: &[u32], b: &[u32]) -> bool

Checks the equality of two slices of primitives in compile-time.

Sourceยง

impl Slice<&[u32]>

Source

pub const fn eq(a: &[&[u32]], b: &[&[u32]]) -> bool

Checks the equality of two slices of slices of primitives in compile-time.

Sourceยง

impl Slice<u64>

Source

pub const fn eq(a: &[u64], b: &[u64]) -> bool

Checks the equality of two slices of primitives in compile-time.

Sourceยง

impl Slice<&[u64]>

Source

pub const fn eq(a: &[&[u64]], b: &[&[u64]]) -> bool

Checks the equality of two slices of slices of primitives in compile-time.

Sourceยง

impl Slice<u128>

Source

pub const fn eq(a: &[u128], b: &[u128]) -> bool

Checks the equality of two slices of primitives in compile-time.

Sourceยง

impl Slice<&[u128]>

Source

pub const fn eq(a: &[&[u128]], b: &[&[u128]]) -> bool

Checks the equality of two slices of slices of primitives in compile-time.

Sourceยง

impl Slice<usize>

Source

pub const fn eq(a: &[usize], b: &[usize]) -> bool

Checks the equality of two slices of primitives in compile-time.

Sourceยง

impl Slice<&[usize]>

Source

pub const fn eq(a: &[&[usize]], b: &[&[usize]]) -> bool

Checks the equality of two slices of slices of primitives in compile-time.

Sourceยง

impl Slice<i8>

Source

pub const fn eq(a: &[i8], b: &[i8]) -> bool

Checks the equality of two slices of primitives in compile-time.

Sourceยง

impl Slice<&[i8]>

Source

pub const fn eq(a: &[&[i8]], b: &[&[i8]]) -> bool

Checks the equality of two slices of slices of primitives in compile-time.

Sourceยง

impl Slice<i16>

Source

pub const fn eq(a: &[i16], b: &[i16]) -> bool

Checks the equality of two slices of primitives in compile-time.

Sourceยง

impl Slice<&[i16]>

Source

pub const fn eq(a: &[&[i16]], b: &[&[i16]]) -> bool

Checks the equality of two slices of slices of primitives in compile-time.

Sourceยง

impl Slice<i32>

Source

pub const fn eq(a: &[i32], b: &[i32]) -> bool

Checks the equality of two slices of primitives in compile-time.

Sourceยง

impl Slice<&[i32]>

Source

pub const fn eq(a: &[&[i32]], b: &[&[i32]]) -> bool

Checks the equality of two slices of slices of primitives in compile-time.

Sourceยง

impl Slice<i64>

Source

pub const fn eq(a: &[i64], b: &[i64]) -> bool

Checks the equality of two slices of primitives in compile-time.

Sourceยง

impl Slice<&[i64]>

Source

pub const fn eq(a: &[&[i64]], b: &[&[i64]]) -> bool

Checks the equality of two slices of slices of primitives in compile-time.

Sourceยง

impl Slice<i128>

Source

pub const fn eq(a: &[i128], b: &[i128]) -> bool

Checks the equality of two slices of primitives in compile-time.

Sourceยง

impl Slice<&[i128]>

Source

pub const fn eq(a: &[&[i128]], b: &[&[i128]]) -> bool

Checks the equality of two slices of slices of primitives in compile-time.

Sourceยง

impl Slice<isize>

Source

pub const fn eq(a: &[isize], b: &[isize]) -> bool

Checks the equality of two slices of primitives in compile-time.

Sourceยง

impl Slice<&[isize]>

Source

pub const fn eq(a: &[&[isize]], b: &[&[isize]]) -> bool

Checks the equality of two slices of slices of primitives in compile-time.

Sourceยง

impl Slice<f32>

Source

pub const fn eq(a: &[f32], b: &[f32]) -> bool

Checks the equality of two slices of primitives in compile-time.

Sourceยง

impl Slice<&[f32]>

Source

pub const fn eq(a: &[&[f32]], b: &[&[f32]]) -> bool

Checks the equality of two slices of slices of primitives in compile-time.

Sourceยง

impl Slice<f64>

Source

pub const fn eq(a: &[f64], b: &[f64]) -> bool

Checks the equality of two slices of primitives in compile-time.

Sourceยง

impl Slice<&[f64]>

Source

pub const fn eq(a: &[&[f64]], b: &[&[f64]]) -> bool

Checks the equality of two slices of slices of primitives in compile-time.

Sourceยง

impl Slice<bool>

Source

pub const fn eq(a: &[bool], b: &[bool]) -> bool

Checks the equality of two slices of primitives in compile-time.

Sourceยง

impl Slice<&[bool]>

Source

pub const fn eq(a: &[&[bool]], b: &[&[bool]]) -> bool

Checks the equality of two slices of slices of primitives in compile-time.

Sourceยง

impl Slice<char>

Source

pub const fn eq(a: &[char], b: &[char]) -> bool

Checks the equality of two slices of primitives in compile-time.

Sourceยง

impl Slice<&[char]>

Source

pub const fn eq(a: &[&[char]], b: &[&[char]]) -> bool

Checks the equality of two slices of slices of primitives in compile-time.

Sourceยง

impl Slice<f16>

Source

pub const fn eq(a: &[f16], b: &[f16]) -> bool

Checks the equality of two slices of primitives in compile-time.

Sourceยง

impl Slice<&[f16]>

Source

pub const fn eq(a: &[&[f16]], b: &[&[f16]]) -> bool

Checks the equality of two slices of slices of primitives in compile-time.

Sourceยง

impl Slice<f128>

Source

pub const fn eq(a: &[f128], b: &[f128]) -> bool

Checks the equality of two slices of primitives in compile-time.

Sourceยง

impl Slice<&[f128]>

Source

pub const fn eq(a: &[&[f128]], b: &[&[f128]]) -> bool

Checks the equality of two slices of slices of primitives in compile-time.

Sourceยง

impl Slice<&str>

ยงMethods for string slices.

Source

pub const fn eq(a: &str, b: &str) -> bool

Checks the equality of two string slices in compile-time.

Sourceยง

impl Slice<&[&str]>

Source

pub const fn eq(a: &[&str], b: &[&str]) -> bool

Checks the equality of two slices of string slices in compile-time.

Trait Implementationsยง

Sourceยง

impl<T: Debug> Debug for Slice<T>

Sourceยง

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

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> Any for T
where T: 'static + ?Sized,

Sourceยง

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Sourceยง

impl<T> AnyExt for T
where T: Any + ?Sized,

Sourceยง

fn type_id() -> TypeId

Returns the TypeId of Self. Read more
Sourceยง

fn type_of(&self) -> TypeId

Returns the TypeId of self. Read more
Sourceยง

fn type_name(&self) -> &'static str โ“˜

Returns the type name of self. Read more
Sourceยง

fn type_is<T: 'static>(&self) -> bool

Returns true if Self is of type T. Read more
Sourceยง

fn type_hash(&self) -> u64

Returns a deterministic hash of the TypeId of Self.
Sourceยง

fn type_hash_with<H: Hasher>(&self, hasher: H) -> u64

Returns a deterministic hash of the TypeId of Self using a custom hasher.
Sourceยง

fn as_any_ref(&self) -> &dyn Any
where Self: Sized,

Upcasts &self as &dyn Any. Read more
Sourceยง

fn as_any_mut(&mut self) -> &mut dyn Any
where Self: Sized,

Upcasts &mut self as &mut dyn Any. Read more
Sourceยง

fn as_any_box(self: Box<Self>) -> Box<dyn Any>
where Self: Sized,

Available on crate feature alloc only.
Upcasts Box<self> as Box<dyn Any>. Read more
Sourceยง

fn downcast_ref<T: 'static>(&self) -> Option<&T> โ“˜

Available on crate feature unsafe_layout and non-crate feature safe_code only.
Returns some shared reference to the inner value if it is of type T. Read more
Sourceยง

fn downcast_mut<T: 'static>(&mut self) -> Option<&mut T> โ“˜

Available on crate feature unsafe_layout and non-crate feature safe_code only.
Returns some exclusive reference to the inner value if it is of type T. Read more
Sourceยง

impl<T> Borrow<T> for T
where T: ?Sized,

Sourceยง

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Sourceยง

impl<T> BorrowMut<T> for T
where T: ?Sized,

Sourceยง

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Sourceยง

impl<T> ByteSized for T

Sourceยง

const BYTE_ALIGN: usize = _

The alignment of this type in bytes.
Sourceยง

const BYTE_SIZE: usize = _

The size of this type in bytes.
Sourceยง

fn byte_align(&self) -> usize

Returns the alignment of this type in bytes.
Sourceยง

fn byte_size(&self) -> usize

Returns the size of this type in bytes. Read more
Sourceยง

fn ptr_size_ratio(&self) -> [usize; 2]

Returns the size ratio between Ptr::BYTES and BYTE_SIZE. Read more
Sourceยง

impl<T> From<T> for T

Sourceยง

fn from(t: T) -> T

Returns the argument unchanged.

Sourceยง

impl<T> Hook for T

Sourceยง

fn hook<F>(self, f: F) -> Self
where F: FnOnce(&mut Self),

Hooks a mutation step into the value and returns it. Read more
Sourceยง

fn tap<F>(self, f: F) -> Self
where F: FnOnce(&Self),

Taps into the value for observation and returns it unchanged. Read more
Sourceยง

impl<T, U> Into<U> for T
where U: From<T>,

Sourceยง

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Sourceยง

impl<T> MemExt for T
where T: ?Sized,

Sourceยง

const NEEDS_DROP: bool = _

Know whether dropping values of this type matters, in compile-time.
Sourceยง

fn mem_align_of<T>() -> usize

Returns the minimum alignment of the type in bytes. Read more
Sourceยง

fn mem_align_of_val(&self) -> usize

Returns the alignment of the pointed-to value in bytes. Read more
Sourceยง

fn mem_size_of<T>() -> usize

Returns the size of a type in bytes. Read more
Sourceยง

fn mem_size_of_val(&self) -> usize

Returns the size of the pointed-to value in bytes. Read more
Sourceยง

fn mem_copy(&self) -> Self
where Self: Copy,

Bitwise-copies a value. Read more
Sourceยง

fn mem_needs_drop(&self) -> bool

Returns true if dropping values of this type matters. Read more
Sourceยง

fn mem_drop(self)
where Self: Sized,

Drops self by running its destructor. Read more
Sourceยง

fn mem_forget(self)
where Self: Sized,

Forgets about self without running its destructor. Read more
Sourceยง

fn mem_replace(&mut self, other: Self) -> Self
where Self: Sized,

Replaces self with other, returning the previous value of self. Read more
Sourceยง

fn mem_take(&mut self) -> Self
where Self: Default,

Replaces self with its default value, returning the previous value of self. Read more
Sourceยง

fn mem_swap(&mut self, other: &mut Self)
where Self: Sized,

Swaps the value of self and other without deinitializing either one. Read more
Sourceยง

unsafe fn mem_zeroed<T>() -> T

Available on crate feature unsafe_layout only.
Returns the value of type T represented by the all-zero byte-pattern. Read more
Sourceยง

unsafe fn mem_transmute_copy<Src, Dst>(src: &Src) -> Dst

Available on crate feature unsafe_layout only.
Returns the value of type T represented by the all-zero byte-pattern. Read more
Sourceยง

fn mem_as_bytes(&self) -> &[u8] โ“˜
where Self: Sync + Unpin,

Available on crate feature unsafe_slice only.
View a Sync + Unpin self as &[u8]. Read more
Sourceยง

fn mem_as_bytes_mut(&mut self) -> &mut [u8] โ“˜
where Self: Sync + Unpin,

Available on crate feature unsafe_slice only.
View a Sync + Unpin self as &mut [u8]. Read more
Sourceยง

impl<T, R> Morph<R> for T
where T: ?Sized,

Sourceยง

fn morph<F>(self, f: F) -> R
where F: FnOnce(Self) -> R, Self: Sized,

Morphs the value into a new one and returns it. Read more
Sourceยง

fn morph_ref<F>(&self, f: F) -> R
where F: FnOnce(&Self) -> R,

Morphs the value by shared reference and returns the result. Read more
Sourceยง

fn morph_mut<F>(&mut self, f: F) -> R
where F: FnOnce(&mut Self) -> R,

Morphs the value by exclusive reference and returns the result. Read more
Sourceยง

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Sourceยง

type Error = Infallible

The type returned in the event of a conversion error.
Sourceยง

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error> โ“˜

Performs the conversion.
Sourceยง

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Sourceยง

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Sourceยง

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error> โ“˜

Performs the conversion.