It dawned on me how useful it would be to hypothetically be able to both mutate and map a Vec<Vec<T>> into an owned type Vec<Vec<U>> in a single iteration and without cloning.
Example: first sort and dedupVec<T> before mapping into Vec<U>
I could only think of a way to do this involving two iterations.
To mutate, call: iter_mut() then mutate each Vec<T> (because of borrow, don't map)
To map, call: into_iter() (to avoid borrow) and then map into a newly owned Vec<U>
Is there a way to do it with only 1 iteration without cloning? If such a way does exist, why wasn't it called into_iter_mut? And if not, why can't it exist feasibly?
There are 3 basic ways to “pass a value” in Rust, each of which has their own method (by convention or by trait):
By reference: &T (conventional iterator method .iter())
By mutable reference: &mut T (conventional iterator method .iter_mut())
By move: T (IntoIterator method .into_iter())
In the type system, “move with mutation” is not a different thing from “move”, so it doesn't have separate methods. A move means the recipient of the moved value gets to do whatever with it, including mutating it.
It's worth adding a note that std provides some special implementation(s) which allows this .into_iter().map(_).collect() pattern for Vecs to remain in-place and not require reallocating into a new Vec allocation.
Not that any cloning would be involved without this specialization, but it does reduce the amount of moves/copying necessary.
Yes, and it would almost certainly be unsound if not. Note that Rust requires passing the same size/align layout when deallocating as when the allocation was made, so recollecting to a different required alignment must create a fresh allocation and mustn't reuse the old one, as that would be unsound. (Even if the alignment requirement is lower!)
Don't rely on this happening for correctness — it's not guaranteed — but it does happen most of the time it's possible as a performance optimization, so it's generally not necessary to do manually.