Trait SliceMut

Source
pub trait SliceMut: Slice {
    // Required method
    fn get_mut(&mut self, index: usize) -> Option<&mut Self::Output>;

    // Provided methods
    fn split_mut(
        &mut self,
        at: usize,
    ) -> Option<(SplitMut<'_, Self>, SplitMut<'_, Self>)>
       where Self: Unique { ... }
    fn copy_from_slice<S>(&mut self, src: &S)
       where S: Slice<Output = Self::Output>,
             S::Output: Clone { ... }
}
Expand description

A Slice that can return mutably borrowed values.

Required Methods§

Source

fn get_mut(&mut self, index: usize) -> Option<&mut Self::Output>

Index the slice, returning a mutably borrowed value.

Provided Methods§

Source

fn split_mut( &mut self, at: usize, ) -> Option<(SplitMut<'_, Self>, SplitMut<'_, Self>)>
where Self: Unique,

Returns (&mut self[..at], &mut self[at..]). Returns None if at is out-of-bounds.

Analagous to slice::split_mut.

To avoid aliasing, requires Self: Unique.

§Examples
let mut slice = [1, 2, 3, 4, 5];
let (mut left, mut right) = SliceMut::split_mut(&mut slice, 2).unwrap();

assert_eq!(left, [1, 2, 3]);
assert_eq!(right, [4, 5]);

left[0] = 0;
right[0] = 0;
assert_eq!(slice, [0, 2, 3, 0, 5]);
Source

fn copy_from_slice<S>(&mut self, src: &S)
where S: Slice<Output = Self::Output>, S::Output: Clone,

Copy all the items from src into self.

Similar to slice::clone_from_slice.

§Panics

The lengths must match. If you only want to copy a sub-slice, you can slice each side down to the desired range.

§Examples
let mut x = [1, 2, 3, 4, 5];
let y = [6, 7];

(&mut x).slice(3..).unwrap().copy_from_slice(&y);
 
assert_eq!(x, [1, 2, 3, 6, 7]);

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<'a, S> SliceMut for &'a mut S
where S: SliceMut + ?Sized,

Source§

fn get_mut(&mut self, index: usize) -> Option<&mut Self::Output>

Source§

impl<T> SliceMut for [T]

Source§

fn get_mut(&mut self, index: usize) -> Option<&mut Self::Output>

Source§

impl<T, const N: usize> SliceMut for [T; N]

Source§

fn get_mut(&mut self, index: usize) -> Option<&mut Self::Output>

Implementors§

Source§

impl<A> SliceMut for SliceOf<A>
where A: SliceMut,

Source§

impl<A> SliceMut for SplitMut<'_, A>
where A: SliceMut + Unique,

Source§

impl<S1, S2> SliceMut for Chain<S1, S2>
where S1: SliceMut, S2: SliceMut<Output = S1::Output>,

Source§

impl<S1, S2> SliceMut for Interleave<S1, S2>
where S1: SliceMut, S2: SliceMut<Output = S1::Output>,

Source§

impl<S> SliceMut for Cycle<S>
where S: SliceMut,

Source§

impl<S> SliceMut for Reverse<S>
where S: SliceMut,