22

I am writing a function of the following format:

fn pop<T>(data: &mut Vec<Option<T>>) -> Option<T> {
    // Let the item be the current element at head
    let item = data[0];

    // and "remove" it.
    data[0] = None;

    item
}

When I try to do this, I get an error which makes sense:

error[E0507]: cannot move out of index of `std::vec::Vec<std::option::Option<T>>`
 --> src/lib.rs:3:16
  |
3 |     let item = data[0];
  |                ^^^^^^^ move occurs because value has type `std::option::Option<T>`, which does not implement the `Copy` trait
  |
help: consider borrowing the `Option`'s content
  |
3 |     let item = data[0].as_ref();
  |                ^^^^^^^^^^^^^^^^
help: consider borrowing here
  |
3 |     let item = &data[0];
  |                ^^^^^^^^

When I try to change it such that item is a reference, I get an error when I try to set data[0] to None, which also makes sense.

Is there some way I can do what I want to do? It seems to me that, whether I want to return a reference or not, I'm going to have to take ownership of the element from the Vec.

I noticed that Vec has a swap_remove method, which does almost exactly what I want, except that it swaps with an element already in the Vec, not with any arbitrary value as I would like. I know that I could just append None to the end of the Vec and use swap_remove, but I'm interested in seeing if there's another way.

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
Cameron Sun
  • 423
  • 1
  • 4
  • 9

1 Answers1

30

Use std::mem::replace:

use std::mem;

fn pop<T>(data: &mut Vec<Option<T>>) -> Option<T> {
    mem::replace(&mut data[0], None)
}

replace essentially replaces the value in a particular location with another one and returns the previous value.

See also:

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
Francis Gagné
  • 60,274
  • 7
  • 180
  • 155
  • 19
    BTW, for replacing an optional value with `None` there is a special method on `Option`, called [`take()`](http://doc.rust-lang.org/core/option/enum.Option.html#method.take). I'd say it is more idiomatic to use it instead of `replace()`, where applicable. – Vladimir Matveev Oct 19 '15 at 05:58
  • 7
    I cannot believe there isn't a method on vec itself – ditoslav Feb 08 '21 at 16:49