You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm writing a fairly standard bytecode interpreter in Rust. The core VM loop looks something like this:
// contains mutable program statestructVM{/* ip, stack, etc. */}implVM{fnstep(&mutself) -> Result<(),&str>{// decode and execute a single potentially fallible bytecode op// oh, an error occured:returnErr("(•_• ?)")}pubfnrun(&mutself) -> Result<(),&str>{// stepping through the programloop{// stop running if something goes wrongself.step()?
}}}
However, this code fails to compile, throwingE0499:
error[E0499]: cannot borrow `*self` as mutable more than once at a time
--> src/lib.rs:15:13
|
11 | pub fn run(&mut self) -> Result<(), &str> {
| - let's call the lifetime of this reference `'1`
...
15 | self.step()?
| ^^^^ - returning this value requires that `*self` is borrowed for `'1`
| |
| mutable borrow starts here in previous iteration of loop
I think I understand why this error is occurring - self.step()? mutably borrows self, and upon the next iteration of the loop, another mutable reference to self can not be taken out. Is there any way to restructure the above code while still maintaining the same behaviour without encountering an error?
One question is particular is very similar to the above situation, however the answers offered were less than satisfactory, as I don't mind changing the structure / signatures of my program to achieve satisfactory results: https://stackoverflow.com/questions/46393890/mutable-borrow-in-loop
That's all :)
Any help or pointers in the right direction are appreciated.
The text was updated successfully, but these errors were encountered:
I'm writing a fairly standard bytecode interpreter in Rust. The core VM loop looks something like this:
However, this code fails to compile, throwing
E0499
:I think I understand why this error is occurring -
self.step()?
mutably borrowsself
, and upon the next iteration of the loop, another mutable reference toself
can not be taken out. Is there any way to restructure the above code while still maintaining the same behaviour without encountering an error?I'm not sure how to proceed - I've refactored the code many times, following suggestions from:
https://stackoverflow.com/questions/39622783/how-can-i-do-a-mutable-borrow-in-a-for-loop
https://stackoverflow.com/questions/36667723/can-i-reset-a-borrow-of-a-local-in-a-loop
https://users.rust-lang.org/t/single-threaded-code-mutable-borrow-in-loop/20879/7
#21906
... And a few other sources I've left out for brevity.
One question is particular is very similar to the above situation, however the answers offered were less than satisfactory, as I don't mind changing the structure / signatures of my program to achieve satisfactory results:
https://stackoverflow.com/questions/46393890/mutable-borrow-in-loop
That's all :)
Any help or pointers in the right direction are appreciated.
The text was updated successfully, but these errors were encountered: