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.
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)
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With