In JavaScript I would do this:
function move(arr, old_index, new_index) {
while (old_index < 0) {
old_index += arr.length;
}
while (new_index < 0) {
new_index += arr.length;
}
if (new_index >= arr.length) {
var k = new_index - arr.length;
while ((k--) + 1) {
arr.push(undefined);
}
}
arr.splice(new_index, 0, arr.splice(old_index, 1)[0]);
return arr;
}
How can I accomplish the same thing in Rust?
I don't want to use insert
and remove
because my vector is a std::vec::Vec<std::string::String>
and I want to literally move them to a different location in the vector, not remove them and then insert a copy.
I don't want to swap 2 elements. I want to change the index of an element to an arbitrary other index, like a person cutting to some arbitrary other position in a queue.