RefCell in Rc (to clone shared reference)

Sorry for maybe stupid question, does ReffCell support shared reference clone to same address in memory (like Rc does) or it's correct to cover RefCell in Rc?

Seems I don't want Rc in case to clone to another thread (as no warnings from analyzer)

Yo do if you need shared ownership to your RefCell, which you usually want. Rc<RefCell<T>> is the classic type you use to get single-threaded interior mutability.

If you want multi-threaded access to the same memory region, you'd have to use thread-safe types like Arc<Mutex<T>> or Arc<RwLock<T>> instead.

1 Like

A RefCell is a Cell, not a Ref, in the same way that a housecat is a cat, not a house. In other words, RefCell isn't a reference or smart pointer, so cloning it doesn't give you "another" reference, whether to the same address or something else. A RefCell<T> contains a T, so cloning it just gives you another RefCell<T> that contains a new, cloned T.

When you .borrow() a RefCell, that gives you a Ref<T>, which is a smart pointer, and can be cloned, although (unlike Rc) it doesn't implement Clone.

1 Like

Thanks for explain, guys

When you .borrow() a RefCell, that gives you a Ref<T>, which is a smart pointer, and can be cloned, although (unlike Rc) it doesn't implement Clone.

Clear!