I was practicing with interior mutability and ran into behavior I cannot explain. Consider the following code:
use std::rc::Rc;
use std::cell::{RefCell, RefMut};
let shared: Rc<RefCell<u8>> = Rc::new(RefCell::new(0));
let mut borrowed = shared.borrow_mut();
*borrowed += 1;
It compiles and borrowed type is inferred as RefMut<u8>.
However, if you add use std::borrow::BorrowMut;, the code does not compile:
error[E0368]: binary assignment operation `+=` cannot be applied to type `Rc<RefCell<u8>>`
--> src/main.rs:9:5
|
9 | *borrowed += 1;
| ---------^^^^^
| |
| cannot use `+=` on type `Rc<RefCell<u8>>`
and borrowed type is inferred as &mut Rc<RefCell<u8>> (if I understand correctly).
I see that both RefCell struct and BorrowMut trait have borrow_mut method, but it's not clear to me how simply use-ing the trait can change what method the compiler chooses.
Likely I don't know some details how method resolution works in Rust compiler. Can anybody help to understand it and/or share pointers where such subtleties are explained?