std-method

slice::split_first_mut

Separate mutable access to the first element and the rest of the sequence, if present. Expected stdout (not executed): [9, 4, 3] Common mistake: Expecting a valid first element when the sequence is empty.

Formal Syntax & Structure

rust syntaxrust
slice.split_first_mut()

Working Example

Example Snippetrust
fn main() {    let mut points = [2, 3, 4];    let (head, rest) = points.split_first_mut().unwrap();    *head = 9;    rest.reverse();    println!("{points:?}");}
Rust Reference