I'm trying to write code that gets the last element of some vector and do different actions (including mutation of the vector) depending on that element.
I tried like this:
#[derive(Clone, PartialEq)]
enum ParseItem {
Start,
End,
}
let mut item_vec = vec![ParseItem::End];
loop {
let last_item = *item_vec.last().clone().unwrap();
match last_item {
ParseItem::End => item_vec.push(ParseItem::Start),
_ => break,
}
}
And I get the following error:
error: cannot move out of borrowed content
let last_item = *item_vec.last().clone().unwrap();
I thought by cloning item_vec.last()
, the problems with ownership would be solved, but it seems not.
If I try the same thing with a vector of integers like this:
let mut int_vec = vec![0];
loop {
let last_int = *int_vec.last().clone().unwrap();
match last_int {
0 => int_vec.push(1),
_ => break,
}
}
the compiler doesn't complain about borrowing.
Why does my code fails to compile?