std-method

slice::split_at_mut_checked

Obtain two nonoverlapping mutable slices if mid is within 0..=len; otherwise get None. This API is useful when the split index comes from input. Expected stdout (not executed): [3, 6, 6] true Common mistake: Rejecting mid equal to len, which is valid and gives an empty right slice.

Formal Syntax & Structure

rust syntaxrust
slice.split_at_mut_checked(mid)

Working Example

Example Snippetrust
fn main() {    let mut scores = [2, 4, 6];    let (left, right) = scores.split_at_mut_checked(1).unwrap();    left[0] += 1;    right[0] += 2;    println!("{scores:?}");    println!("{}", scores.split_at_mut_checked(4).is_none());}
Rust Reference