RefCell+Rc, Rc+RefCell, and Dereferencing
⚓ Rust 📅 2025-11-04 👤 surdeus 👁️ 4I'm trying to better understand some borrowed code by recreating a minimal version that I can follow. But I have no idea if these patterns are sane or idiomatic:
V1: (similar to what I'm using now)
Playground: Link
Reference: Design Patterns in Rust: Mediator
Reference: Wasm_Bindgen: TODO MVC
Has:
Rc<RefCell<Option<T>>>in the mediator field&*self.leaf_1.borrow_mut()
V2: (modifed example from rust docs)
Playground: Link
Reference: Rust Docs: Reference Cycles Can Leak Memory
Has:
RefCell<Rc<Option<T>>>in the branch fieldWeak, Downgrade/Upgrade&**self.leaf_1.borrow_mut()
Both versions compile and lint OK.
What I need help with:
- The
Rc+RefCell / RefCell+Rcthing. -- I don't know if one structure is better than the other for the fields in the Mediator / Branch, or if the ordering will cause problems down the line. &*and&**to get references to the Rc items: -- It took an embarrasing amout of time to figure out how to get these references - but the double / triple operators makes me think I'm not accessing these items properly / idiomatically / explicitly.
Are there any rules of thumb I should be following when constucting these Rc items and getting references to them?
2 posts - 2 participants
🏷️ Rust_feed