I started writing Rust code a few days ago, and just now had my first encounter with the borrow checker.
#[derive(Clone, Eq, Debug, PartialEq)]
pub struct Vm<'a> {
instructions: Rc<InstructionSequence>,
pc: usize,
stack: Vec<Value<'a>>,
frames: Vec<Frame<'a>>,
}
impl<'a> Vm<'a> {
pub fn run(&'a mut self) {
loop {
let instruction = self.instructions.get(self.pc).unwrap();
match instruction {
&Instruction::Push(ref value) => {
let top_activation = &mut self.frames.last_mut().unwrap().activation;
self.stack.push(Vm::literal_to_value(value, top_activation))
},
_ => ()
};
};
}
}
Rust gives me the following errors:
error[E0499]: cannot borrow `self.frames` as mutable more than once at a time
--> src/vm.rs:157:47
|
157 | let top_activation = &mut self.frames.last_mut().unwrap().activation;
| ^^^^^^^^^^^
| |
| second mutable borrow occurs here
| first mutable borrow occurs here
...
181 | }
| - first borrow ends here
error[E0499]: cannot borrow `self.frames` as mutable more than once at a time
--> src/vm.rs:157:47
|
157 | let top_activation = &mut self.frames.last_mut().unwrap().activation;
| ^^^^^^^^^^^
| |
| second mutable borrow occurs here
| first mutable borrow occurs here
...
181 | }
| - first borrow ends here
error: aborting due to 2 previous errors
I don't understand why it's getting borrowed twice. What's going on?