Vec::append method takes a mutable reference to the other vector because it removes the items from it. I wrote a lot of Rust code, but I never needed this exact form of
append. I always want to have a function that consumes a vector. A common example looks like this:
I would very much prefer to write
vec.push_vec(fn_returning_vec()) instead. In another case, if the source vector is stored in a variable, I need to make it
mut if I want to append it to something:
let mut source_vec = ... vec.append(&mut source_vec);
It makes the code worse because the variable would otherwise be immutable, allowing to reason about its value more easily.
I understand that
Vec::append is more capable than a consuming version and it makes sense to have it in
Vec::append allows you to re-use the allocated buffer of the source vector if you need it later. However, consuming the source vector is just so much more common, in my experience.
My questions are:
- Is there a consuming version of
stdor in a widely used crate?
- Why not?
- Did you have a situation when you found
Vec::appenduseful and a consuming version would not fit better?