std-method

Rust slice::split_at_mut(): partition an exclusive slice

This slice method divides one mutable slice into a prefix of length mid and its remaining suffix. It borrows existing elements without allocating or copying them. mid may equal zero or the length; values greater than the length panic. Expected output (not executed): [20, 4, 7] Why: Both writes update separate parts of amounts. Common mistake: Treating mid as the last index of the left slice instead of its length.

Formal Syntax & Structure

rust syntaxrust
pub fn split_at_mut(&mut self, mid: usize) -> (&mut [T], &mut [T])

Parameters & Attributes

NameTypeDescription
midusizeLength of the prefix; must be at most the slice length.

Return Value

(&mut [T], &mut [T])

Two exclusive, nonoverlapping slice views tied to the input borrow.

Working Example

Example Snippetrust
fn main() {    let mut amounts = [2, 4, 6];    let (left, right) = amounts.split_at_mut(1);    left[0] *= 10;    right[1] += 1;    println!("{amounts:?}");}
Rust Reference