Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot borrow `*self` as mutable because `self.history[..]` is also borrowed as immutable

Tags:

rust

The code is something like the following, in a function that is an implementation for a Context struct, defined as the following:

struct Context {
    lines: isize,
    buffer: Vec<String>,
    history: Vec<Box<Instruction>>,
}

And the function, of course as an implementation:

fn _execute_history(&mut self, instruction: &Instruction) -> Reaction {
    let num = instruction.suffix.parse::<usize>();
    match num {
        Ok(number) => {
            match self.history.get(number) {
                Some(ins) => { return self.execute(*ins); },
                _         => { /* Error handling */ }
            }
        }
        Err(..) => { /* Error handling */ }
    }
}

This doesn't compile and I don't understand the error message. I searched online for similar problems and I cannot seem to grasp the problem here. I am from a Python background. The full error:

hello.rs:72:23: 72:35 note: previous borrow of `self.history[..]` occurs here; the immutable
borrow prevents subsequent moves or mutable borrows of `self.history[..]` until the borrow ends

I am fully aware that the function is not conforming to the type system, but this is because the simplified code is for demonstrative purposes only.

like image 916
knight Avatar asked Apr 12 '14 15:04

knight


1 Answers

You borrow a value from self.history with get and that borrow is still "alive" when you call execute on self (that requires &mut self from what I can see from the error)

In this case return your value from the match and call self.execute after the match:

fn _execute_history(&mut self, instruction: &Instruction) -> Reaction {
    let num = instruction.suffix.parse::<usize>();
    let ins = match num {
        Ok(number) => {
            match self.history.get(number) {
                Some(ins) => ins.clone(),
                _         => { /* Error handling */ panic!() }
            }
        }
        Err(..) => { /* Error handling */ panic!() }
    };
    self.execute(ins)
}
like image 166
Arjan Avatar answered Nov 16 '22 01:11

Arjan