What is the difference between Rc> and RefCell>?

后端 未结 1 1187
鱼传尺愫
鱼传尺愫 2020-12-17 00:52

The Rust documentation covers Rc> pretty extensively but doesn\'t go into RefCell>, which I am now encount

相关标签:
1条回答
  • 2020-12-17 01:28

    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>>.

    0 讨论(0)
提交回复
热议问题