THE QUESTION
When are RefCell counted down?
THE CASE
// Working
use std::cell::RefCell;
fn main() {
let username = RefCell::new("");
*username.borrow_mut() = "Genese";
*username.borrow_mut() = "Salathiel";
println!("{}", username.borrow()); // Salatthiel
}
Then...
// !working
use std::cell::RefCell;
fn main() {
let username = RefCell::new("");
let mut username_mutable_borrow = username.borrow_mut();
*username_mutable_borrow = "Genese";
let mut username_mutable_borrow = username.borrow_mut();
*username_mutable_borrow = "Salathiel";
println!("{}", username.borrow());
}
// thread 'main' panicked at 'already borrowed: BorrowMutError', src/main.rs:7:48
I did other experiments with std::ops::Deref without much success or insights.