如何正确访问 RefCell 中的值 [英] How to access value in RefCell properly

查看:48
本文介绍了如何正确访问 RefCell 中的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 Rust 中围绕 RcRefCell.我想要实现的是对同一对象有多个可变引用.

I'm trying to wrap my head around Rc and RefCell in Rust. What I'm trying to achieve is to to have multiple mutable references to the same objects.

我想出了这个虚拟代码:

I came up with this dummy code:

use std::rc::Rc;
use std::cell::RefCell;

struct Person {
    name: String,
    mother: Option<Rc<RefCell<Person>>>,
    father: Option<Rc<RefCell<Person>>>,
    partner: Option<Rc<RefCell<Person>>>
}

pub fn main () {

    let mut susan = Person {
        name: "Susan".to_string(),
        mother: None,
        father: None,
        partner: None
    };

    let mut boxed_susan = Rc::new(RefCell::new(susan));

    let mut john = Person {
        name: "John".to_string(),
        mother: None,
        father: None,
        partner: Some(boxed_susan.clone())
    };

    let mut boxed_john = Rc::new(RefCell::new(john));

    let mut fred = Person {
        name: "Fred".to_string(),
        mother: Some(boxed_susan.clone()),
        father: Some(boxed_john.clone()),
        partner: None
    };

    fred.mother.unwrap().borrow_mut().name = "Susana".to_string();

    println!("{}", boxed_susan.borrow().name);

    // boxed_john.borrow().partner.unwrap().borrow_mut().name = "Susanna".to_string();
    // println!("{}", boxed_susan.borrow().name);

}

最有趣的部分是:

    fred.mother.unwrap().borrow_mut().name = "Susana".to_string();
    println!("{}", boxed_susan.borrow().name)

我更改了 Freds 母亲的名字,然后打印出 Susan 的名字,这恰好是完全相同的参考.令人惊讶的是,它打印出Susana",所以我假设我共享可变引用的小实验是成功的.

I change the name of Freds mother and then print out the name of Susan which should happen to be exactly the same reference. And surprise, surprise it prints out "Susana" so I am assuming that my little experiment of having shared mutable references was successful.

然而,这次我想再次改变它,以约翰的伙伴身份访问它,这也应该是完全相同的实例.

However, now I wanted to mutate it again this time accessing it as the partner of John which should also happen to be exactly the same instance.

不幸的是,当我在以下两行中发表评论时:

Unfortunately when I comment in the following two lines:

// boxed_john.borrow().partner.unwrap().borrow_mut().name = "Susanna".to_string();
// println!("{}", boxed_susan.borrow().name);

我遇到了我的老朋友无法摆脱对&-pointer的引用.我在这里做错了什么?

I'm running into my old friend cannot move out of dereference of&-pointer. What am I doing wrong here?

推荐答案

这将解决它:

boxed_john.borrow().partner.as_ref().unwrap().borrow_mut().name = "Susanna".to_string();

问题在于 Option> 上的 unwrap(),它消耗了 Option(即移出它),但您只有一个借用的指针.as_refOption(T) 转换为 Option(&T) 并且 unwrap 将其转换为 &T,避免任何移动.

The problem is the unwrap() on the Option<Rc<RefCell>>, which consumes the Option (ie moves out of it), but you only have a borrowed pointer. The as_ref converts the Option(T) to Option(&T) and unwrap converts it to &T, avoiding any move.

另请注意:您的变量具有比它们真正需要的更多的可变性.但我相信你已经看到了编译器警告.

Also note: your variables have a lot more mutability than they really need. But I'm sure you're already seeing the compiler warnings for that.

这篇关于如何正确访问 RefCell 中的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆