My use case involves two mutable vectors a and b and a usize parameter x. I want to make the following change:
- take the elements
b[0..x]and append them to the end ofa(changing capacity ofaas required) - transform
bintob[x..], without changing the original capacity ofb
Currently I do the following:
while a.len() < x && !b.is_empty() {
a.push(b.pop_front().unwrap());
// here `b` is a VecDeque but I am happy to use a Vec if pop_front was not required
}
Obviously this seems a very slow operation, checking two conditions and calling unwrap at every iteration. It would be great if there was a rev_split_off operation such that:
let mut front = b.rev_split_off(x);
a.append(&mut front);
Here rev_split_off returns a newly allocated vector for the slice b[0..x] and transforms b into the remaining slice with unchanged capacity.
Question: How to perform my use case efficiently, with or without using such a thing as rev_split_off?