borrowing
Rust Split a mutable sequence into disjoint regions
Two separate indexing expressions do not generally prove to the borrow checker that their elements differ. Use a safe splitting API when both regions must be borrowed together. The returned views cover nonoverlapping parts of the original sequence. Expected output (not executed): [13, 14] / [16, 20] Why: The splitting operation supplies two disjoint mutable slices, making both loans valid. Common mistake: Writing two &mut values[index] expressions and expecting the compiler to prove index inequality.
Formal Syntax & Structure
rust syntaxrust
let (left, right) = values.split_at_mut(mid);Working Example
Example Snippetrust
fn main() { let mut temperatures = [12, 14, 18, 20]; let (morning, evening) = temperatures.split_at_mut(2); morning[0] += 1; evening[0] -= 2; println!("{morning:?} / {evening:?}");}