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

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.

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)
}

Need Your Help

Fail to load NPAPI plugin in Google Chrome on Mac OS X

macos google-chrome plugins npapi

I have been trying to get Google Chrome (6.0.401.1 dev) on Mac OS X to load an NPAPI plugin without success so far. I have been working around the npsimple example from here: http://git.webvm.net/?p=

Error on attempt to create named pipe using VBScript

windows-7 vbscript named-pipes

I'm trying to create named pipe using VBScript on win7.