Want to get multiple mutable slices from a vector?
Rust will complain about:
let range_a = &mut vec;
let range_b = &mut vec;
Because any mutable loan is exclusive for the entire Vec
. Rust doesn't understand that the ranges are non-overlapping. The solution is to use .split_at_mut()
:
let = vec.split_at_mut;
If you want to completely split a Vec
into sub-ranges, see vec.chunks_mut()
.
This is not an issue with immutable slices, because they are shared loans, and many of them can exist at the same time.
Try it: Rust Playground