Ownership &mut Vec<Foo>


#1
  1. I have this code:
pub struct Foo {}


pub fn good(dst: &mut Vec<Foo>, src: &mut Vec<Foo>) {
    while src.len() != 0 {
        dst.push(src.pop().unwrap())
    }
}


pub fn bad(dst: &mut Vec<Foo>, src: &mut Vec<Foo>) {
    for x in src.into_iter().rev() {
        src.push(x)

    }
}
  1. I would like to write the same function via iterator instead of pop.

  2. How can I do this? The requirements are:

3a. src ends up empty
3b. elements of src are reversed + appended to dst


#2

Actually, using iterators, it looks pretty nice!
Playground

Note that this still uses a copy, which forces your type to be copy
Look at @cuviper’s post below


#3
dst.extend(src.drain(..).rev());