std-method

slice::split_last_mut

Borrow the final element and the preceding elements mutably as separate parts. Unlike pop, this does not remove anything. Expected stdout (not executed): [0, 0, 9] Common mistake: Expecting the length to decrease after the call.

Formal Syntax & Structure

rust syntaxrust
slice.split_last_mut()

Working Example

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