If at runtime
borrow_mut() is called and the return value is in scope at the same time as another
borrow, it will panic (note that there are
try_ variants to handle this at runtime). If you're compiling this in wasm, there is only one thread, but it can happen if you write async code and your borrow stays in scope over an
You might make it take
&mut self to avoid that problem with this struct, however if there are also other structs and closures that borrow this value, that won't save you.
It's not always easy to know what the best design is. I'm working on an actor library, since actors solve this design problem rather conveniently, but it's not published yet. Otherwise you just want to be really clear on your book-keeping and keep the borrows as short as possible.