How to satisfy Rust borrow checker with this simple code?

Viewed 200

I'm getting a Rust compile error from the borrow checker, and don't know how to fix it.
The code below is simple, and no problem with similar code in C++.

fn main() {
    let mut nums = vec![1, 2, 3];
    if let Some(x) = nums.last() {
        nums.push(*x);
    }
}

Here is the error:

message: 'cannot borrow `nums` as mutable because it is also borrowed as immutable (4, 9)'
2 Answers
Related