I would like to get a slice of references from a vector which contains all but the N
th element. In my specific case, I would like a mutable reference to the N
th element and non-mutable references to all other elements.
I've got something that works but it results in some messy code.
let len = vector.len();
for index in 0..len {
let (before, after_inclusive) = vector.split_at_mut(index);
let (element_slice, after) = after_inclusive.split_at_mut(1);
let element = &mut element_slice[0];
let others = [before, after].concat();
element.operation(&others);
}
Ideally, I would want a function that works something like this:
let (element, others) = vector.extract_split(index);
Or maybe some mode advanced slice syntax:
let others = vector[0..index, (index+1)..len]
Is there some library or cleaner bit of code that can do this?
For reference, the intent is to modify the position of an object in relation to all other objects. The loop acts as a basic collision detection system within a game.