I had something similar to the code below in a gtk+rust app and thought that might be related to gtk and its internal management of things, but I'm able to reproduce it without it.
The s
variable is a RefCell, and this code panics because of "already borrowed".
// thread 'main' panicked at 'already borrowed: BorrowMutError'
if let Some(_) = s.borrow().get() {
s.borrow_mut().set()
}
Now, to fix it I thought creating a narrower scope for the borrow would work, like this:
// thread 'main' panicked at 'already borrowed: BorrowMutError'
if let Some(_) = { s.borrow().get() } {
s.borrow_mut().set()
}
but it doesn't, although I found othe two options that do work, I'm just curious about why my attempt at creating a scope didn't work.
// works
if let Some(_) = {
let v = s.borrow().get();
v
} {
s.borrow_mut().set()
}
// also works
let v = s.borrow().get();
if let Some(_) = v {
s.borrow_mut().set()
}
thanks y'all, here's a link to a playground with a working (or not) version of this Rust Playground