55

The Rust documentation covers Rc<RefCell<T>> pretty extensively but doesn't go into RefCell<Rc<T>>, which I am now encountering.

Do these effectively give the same result? Is there an important difference between them?

3
  • 6
    which I am now encountering — where are you encountering such a strange type?
    – Shepmaster
    Commented Aug 6, 2019 at 0:03
  • 1
    @Shepmaster I see it (and then see this question) in the BOOK! I am astonished when I see it in the book. Hope I misunderstand something doc.rust-lang.org/book/ch15-06-reference-cycles.html
    – ch271828n
    Commented Sep 12, 2020 at 14:11
  • Ok I do know it. In that example the (uncommon) usage is correct and non replaceable.
    – ch271828n
    Commented Sep 12, 2020 at 14:40

1 Answer 1

96
+100

Do these effectively give the same result?

They are very different.

Rc is a pointer with shared ownership while RefCell provides interior mutability. The order in which they are composed makes a big difference to how they can be used.

Usually, you compose them as Rc<RefCell<T>>; the whole thing is shared and each shared owner gets to mutate the contents. The effect of mutating the contents will be seen by all of the shared owners of the outer Rc because the inner data is shared.

You can't share a RefCell<Rc<T>> except by reference, so this configuration is more limited in how it can be used. In order to mutate the inner data, you would need to mutably borrow from the outer RefCell, but then you'd have access to an immutable Rc. The only way to mutate it would be to replace it with a completely different Rc. For example:

let a = Rc::new(1);
let b = Rc::new(2);

let c = RefCell::new(Rc::clone(&a));
let d = RefCell::new(Rc::clone(&a));

*d.borrow_mut() = Rc::clone(&b); // this doesn't affect c

There is no way to mutate the values in a and b. This seems far less useful than Rc<RefCell<T>>.

2
  • I see. Does that mean RefCell<Rc is when you want to be able to mutate whatever Rc is pointing to?
    – Gman man
    Commented Aug 5, 2019 at 23:56
  • 6
    It would be for when you have one Rc and I want to replace it with a different one. None of the clones of the original Rc would be affected.
    – Peter Hall
    Commented Aug 5, 2019 at 23:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.