Guilherme Marthe
Guilherme Marthe

Reputation: 1124

Shouldn't borrowed values end after an expression?

I'm trying to figure out why the following code doesn't compile.

The desired behavior is: given an integer vector, find its greatest value and push a value that is the greatest value + 1 onto the same vector.

fn main() {
    let mut vector = vec![1, 3, 2];
    let greatest_value;
    { // 1
        let mut gv = &vector[0]; // 2
        for x in &vector {
            if x > gv {
                gv = x;
            }
        }
        greatest_value = gv;
    } // 3
    vector.push(greatest_value + 1);
    println!("O maior é: {}", greatest_value);
}

The delicate part is that the push method from the Vec class needs a mutable borrow in order to modify the self instance vector. The compiler sees that I did an immutable borrow in the line commented with // 2:

error[E0502]: cannot borrow `vector` as mutable because it is also borrowed as immutable
  --> src/main.rs:13:5
   |
5  |         let mut gv = &vector[0]; // 2
   |                       ------ immutable borrow occurs here
...
13 |     vector.push(greatest_value + 1);
   |     ^^^^^^ mutable borrow occurs here
14 |     println!("O maior é: {}", greatest_value);
15 | }
   | - immutable borrow ends here

Shouldn't that borrow have ended at the line commented with // 3?

Upvotes: 2

Views: 106

Answers (1)

loganfsmyth
loganfsmyth

Reputation: 161447

You're missing one small detail, which is that the type of gv (and thus greatest_value) is &i32, not i32, so greatest_value is holding a reference to vector.

You'll want to do

greatest_value = *gv;

to store just the numeric value.

You can also simplify your logic to avoid needing to leave the variable uninitialized that way, which can help readability, e.g.

let greatest_value = {
    let mut gv = &vector[0];
    for x in &vector {
        if x > gv {
            gv = x;
        }
    }
    *gv
};

However really, you can simplify your code a bit with

let greatest_value = *vector.iter().max().unwrap();

if you'd like.

Upvotes: 8

Related Questions