When you call .last() , you take nums as immutable, since mutation will invalidate the x link you are holding. Then you call .push , which borrows nums as mutable.
The problem is that you now have an unchangeable and mutable borrowing of the same value, which contradicts the rust memory safety guarantees (several readers or one copyright guarantee ensures that you never have invalid memory anywhere).
fn main() { let mut nums = vec![1, 2, 3]; if let Some(x) = nums.last() {
The solution would be to reduce the volume of immutable borrowing by immediately dropping the link to its result, according to the example of @DanielSanchez:
let mut nums = vec![1, 2, 3]; if let Some(&x) = nums.last() {
source share