borrowing
Rust Edit a borrowed slice in place
A mutable slice lets an operation modify a region of existing elements. It does not own those elements and cannot grow the owning container. Use it when the operation works on a fixed-length borrowed region. Expected output (not executed): [3, 10, 14] Why: Only the selected suffix is modified. Each iteration supplies an exclusive element reference. Common mistake: Expecting a mutable slice to support Vec::push or to own a resized collection.
Formal Syntax & Structure
rust syntaxrust
fn update(items: &mut [T]) { ... }Working Example
Example Snippetrust
fn double(items: &mut [i32]) { for item in items { *item *= 2; }}fn main() { let mut prices = [3, 5, 7]; double(&mut prices[1..]); println!("{prices:?}");}