It is impossible to take `` self`` as mutable, because `self.history [..]` is also borrowed as immutable`

In the function representing the implementation for the structure Context, the following code looks like this:

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

And the function, of course, is like 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 does not compile and I do not understand the error message. I searched online for similar problems and I cannot understand the problem here. I am in the background of Python. 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 fully understand that the function does not correspond to the type system, but this is due to the fact that the simplified code is intended for demonstration purposes only.

+4
source share
1 answer

self.history get, - "", execute self ( &mut self , )

self.execute :

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

Source: https://habr.com/ru/post/1536243/


All Articles