Rust tips is a user on octodon.social. You can follow them or interact with them if you have an account anywhere in the fediverse. If you don't, you can sign up here.
Rust tips @rust

Want to get multiple mutable slices from a vector?

will complain about:

let range_a = &mut vec[0..10];
let range_b = &mut vec[10..];

Because the first mutable borrow is for the entire `Vec`. Rust doesn't understand that the ranges are non-overlapping. The solution is to use `.split_at_mut()`:

let (range_a, range_b) = vec.split_at_mut(10);

If you want to completely split a Vec into sub-ranges, see `vec.chunks_mut()`.

Try it: is.gd/Q5c8vD

· Web · 3 · 9