I am writing a function in the following format:
fn pop(data: &mut Vec<Option<T>>) -> Option<T> {
let item = data[0];
data[0] = None;
item
}
When I try to do this, I get an error "cannot exit indexed content", which makes sense. When I try to change it in such a way that item
is a link, I get an error when I try to set data[0]
in None
, which also makes sense.
Is there a way to do what I want to do? It seems to me that whether I want to return the link or not, I will have to take responsibility for the item from Vec.
I noticed that Vec has a method swap_remove
that does almost what I want, except that it changes with the element already in Vec, and not with any arbitrary value as I would like. I know that I could just add None
Vec to the end and use it swap_remove
, but I am interested to know if there is another way.
source
share