interior-mutability

What is the difference between Rc<RefCell<T>> and RefCell<Rc<T>>?

谁都会走 提交于 2019-12-02 09:21:37
问题 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? 回答1: 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

What is the difference between Rc<RefCell<T>> and RefCell<Rc<T>>?

删除回忆录丶 提交于 2019-12-02 07:41:58
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? 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

How do I return an iterator that has a reference to something inside a RefCell?

醉酒当歌 提交于 2019-11-28 09:40:27
问题 I'm trying to create a method that returns an iterator over the values of HashMap that is boxed inside a RefCell , but i'm having an error where Ref returned by RefCell::borrow doesn't live long enough for iterator to be returned from the method. Here's my code: use std::rc::Rc; use std::cell::RefCell; use std::collections::HashMap; use std::collections::hash_map::Values; struct Foo { map: Rc<RefCell<HashMap<i32, i32>>>, } impl Foo { fn iter(&self) -> Values<i32, i32> { self.map.borrow()

Situations where Cell or RefCell is the best choice

旧城冷巷雨未停 提交于 2019-11-27 02:08:00
When would you be required to use Cell or RefCell ? It seems like there are many other type choices that would be suitable in place of these, and the documentation warns that using RefCell is a bit of a "last resort". Is using these types a " code smell "? Can anyone show an example where using these types makes more sense than using another type, such as Rc or even Box ? Vladimir Matveev It is not entirely correct to ask when Cell or RefCell should be used over Box and Rc because these types solve different problems. Indeed, more often than not RefCell is used together with Rc in order to

How do I borrow a RefCell<HashMap>, find a key, and return a reference to the result? [duplicate]

99封情书 提交于 2019-11-26 16:43:18
This question already has an answer here: How do I return a reference to something inside a RefCell without breaking encapsulation? 3 answers I have a RefCell<HashMap> and want to borrow the table, find a key, and return a reference to the result: use std::cell::RefCell; use std::collections::HashMap; struct Frame { map: RefCell<HashMap<String, String>>, } impl Frame { fn new() -> Frame { Frame { map: RefCell::new(HashMap::new()), } } fn lookup<'a>(&'a self, k: &String) -> Option<&'a String> { self.map.borrow().get(k) } } fn main() { let f = Frame::new(); println!("{}", f.lookup(&"hello".to

How do I return a reference to something inside a RefCell without breaking encapsulation?

北城余情 提交于 2019-11-26 14:43:00
I have a struct that has inner mutability. use std::cell::RefCell; struct MutableInterior { hide_me: i32, vec: Vec<i32>, } struct Foo { //although not used in this particular snippet, //the motivating problem uses interior mutability //via RefCell. interior: RefCell<MutableInterior>, } impl Foo { pub fn get_items(&self) -> &Vec<i32> { &self.interior.borrow().vec } } fn main() { let f = Foo { interior: RefCell::new(MutableInterior { vec: Vec::new(), hide_me: 2, }), }; let borrowed_f = &f; let items = borrowed_f.get_items(); } Produces the error: error[E0597]: borrowed value does not live long

How do I borrow a RefCell<HashMap>, find a key, and return a reference to the result? [duplicate]

我的未来我决定 提交于 2019-11-26 04:55:05
问题 This question already has an answer here: How do I return a reference to something inside a RefCell without breaking encapsulation? 3 answers I have a RefCell<HashMap> and want to borrow the table, find a key, and return a reference to the result: use std::cell::RefCell; use std::collections::HashMap; struct Frame { map: RefCell<HashMap<String, String>>, } impl Frame { fn new() -> Frame { Frame { map: RefCell::new(HashMap::new()), } } fn lookup<\'a>(&\'a self, k: &String) -> Option<&\'a

How do I return a reference to something inside a RefCell without breaking encapsulation?

谁说胖子不能爱 提交于 2019-11-26 02:37:57
问题 I have a struct that has inner mutability. use std::cell::RefCell; struct MutableInterior { hide_me: i32, vec: Vec<i32>, } struct Foo { //although not used in this particular snippet, //the motivating problem uses interior mutability //via RefCell. interior: RefCell<MutableInterior>, } impl Foo { pub fn get_items(&self) -> &Vec<i32> { &self.interior.borrow().vec } } fn main() { let f = Foo { interior: RefCell::new(MutableInterior { vec: Vec::new(), hide_me: 2, }), }; let borrowed_f = &f; let