rust - Cannot borrow `*self` as mutable because `self.history[..]` is also borrowed as immutable` -
the code following, in function implementation context
struct, defined following:
struct context { lines: isize, buffer: vec<string>, history: vec<box<instruction>>, }
and function, of course 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 , don't understand error message. searched online similar problems , cannot seem grasp problem here. python background. full error:
hello.rs:72:23: 72:35 note: previous borrow of `self.history[..]` occurs here; immutable borrow prevents subsequent moves or mutable borrows of `self.history[..]` until borrow ends
i aware function not conforming type system, because simplified code demonstrative purposes only.
you borrow value self.history
get
, borrow still "alive" when call execute
on self
(that requires &mut self
can see error)
in case return value match , call self.execute
after 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) }
Comments
Post a Comment