so I'm relatively new in Rust and I was trying to get something similair to a std::shared_ptr in C++. I decided to go with the Rc<RefCell> pattern.
I'm trying to get and modify the value of Rc<RefCell<i32>>
but borrow_mut()
keeps returning &mut Rc<RefCell<i32>>
instead of MutRef<i32>
I'm working on 2 projects currently. In the first project test_mut
is of type RefMut<i32>
.
let mut test: Rc<RefCell<i32>> = Rc::new(RefCell::new(5));
let test_mut = test.borrow_mut();
But in my other project test_mut
is of type &mut Rc<RefCell<i32>>
.
WHYYY??
When I don't let the compiler deduct the type and replace the code with:
let mut test: Rc<RefCell<i32>> = Rc::new(RefCell::new(5));
let test_mut: RefMut<i32> = test.borrow_mut();
I get the following error:
mismatched types
expected struct `RefMut<'_, i32>`
found mutable reference `&mut Rc<RefCell<i32>>`
If anyone has any idea how I can prevent this, you would be my hero :)
std::borrow::BorrowMut
instead of the one implemented by theRefCell
. Just delete the import and it should work correctly. – Jaffe