2
votes

I'm having trouble understanding why I can't use v a second time, when it seems that the first mutable borrow has gone out of scope:

fn get_or_insert(v: &mut Vec<Option<i32>>, index: usize, default: i32) -> &mut i32 {
    if let Some(entry) = v.get_mut(index) { // <-- first borrow here
        if let Some(value) = entry.as_mut() {
            return value;
        }
    }

    // error[E0502]: cannot borrow `*v` as immutable because it is also borrowed as mutable    
    while v.len() <= index {                // <-- compiler error here
        v.push(None);
    }

    // error[E0499]: cannot borrow `*v` as mutable more than once at a time
    let entry = v.get_mut(index).unwrap();  // <-- compiler error here
    *entry = Some(default);
    entry.as_mut().unwrap()
}

playground link

Do I have my variable scopes wrong, or is the borrow checker protecting me from something I'm not seeing?

Edit: the error message with NLL enabled is pretty good:

error[E0502]: cannot borrow `*v` as immutable because it is also borrowed as mutable
  --> src/main.rs:10:11
   |
3  | fn get_or_insert(v: &mut Vec<Option<i32>>, index: usize, default: i32) -> &mut i32 {
   |                     - let's call the lifetime of this reference `'1`
4  |     if let Some(entry) = v.get_mut(index) {
   |                          - mutable borrow occurs here
5  |         if let Some(value) = entry.as_mut() {
6  |             return value;
   |                    ----- returning this value requires that `*v` is borrowed for `'1`
...
10 |     while v.len() <= index {
   |           ^ immutable borrow occurs here
1
enable nll give a more precise error; play.rust-lang.org/…Stargateur
I can't explain to you why it doesn't work, but here a solution, play.rust-lang.org/…. I wonder if we could add this to std, but I don't see any use case of this on a Vec. I think a hashmap would be more suitable for this kind of thing but without know all detail from your project it's impossible to be sure. doc.rust-lang.org/std/collections/hash_map/…Stargateur
You aren't missing anything here. Your code is actually safe, but the borrow checker can't handle this case. The type checker infers a single lifetime for the first borrow, and that lifetime must be long enough to return value. There aren't two different lifetimes for both branches of the if let.Sven Marnach

1 Answers

1
votes

The key point is that, even with NLL, the lifetime of the return value spans the whole function. The fact that the function returns early on line 4 is not taken into account when deciding whether the v reference is accessible in the code lower down.

The fix suggested by @Stargateur is to grow the vector if needed before accessing an element:

fn get_or_insert(v: &mut Vec<Option<i32>>, index: usize, value: i32) -> &mut i32 {
    if v.len() < index {
        v.resize(index + 1, None);
    }
    v[index].get_or_insert(value)
}

playground link

Here's where I used the technique in the final code.