“Unwrapping” Rc> in the proper way

huangapple go评论56阅读模式
英文:

"Unwrapping" Rc<RefCell<T>> in the proper way

问题

Here's the translated code without the parts you mentioned not to translate:

// 一些我需要在代码的不同位置传递并希望能够从中操作的中心数据结构。
struct MyState {
    data: i32 // 一些数据
}

// 仅仅是一个修改示例状态的方法。
impl MyState {
    fn modify_state(&mut self, data: i32) {
        self.data += data;
    }
}

// 为了能够传递这个结构,我将它放入 RefCell 进行内部可变性,然后使用 Rc 进行引用计数。
// 为了方便,我将一切都包装在一个新类型中。
#[derive(Clone)]
struct MyStateRef(Rc<RefCell<MyState>>);

impl MyStateRef {
    fn new() -> Self {
        MyStateRef(Rc::new(RefCell::new(MyState{data: 0})))
    }
}

fn main() {
    // 创建一个包装在 Rc 和 RefCell 中的 MyState 实例
    let state = MyStateRef::new();

    // 现在可以克隆并传递 "state"。在某个时刻,我想通过调用 modify_state 来修改内部的 MyState。
    state.0.borrow_mut().modify_state(3);
}

I've removed the untranslated parts as you requested.

英文:

I have a Rust data structure that I will have to pass around and manipulate from various places in my code. To my understanding the most simple and default way to do this is to wrap everything in Rc for the reference counting and a RefCell for interior mutability. I at least think that I have basic understanding of both structs and came up with the following approach:

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

//  Some central data structure I would have to pass around and would like to manipulate from
//  different locations in my code.
struct MyState {
    data: i32 //  Some data
}

//  Just an example method that will mutate my sample state.
impl MyState {
    fn modify_state(&amp;mut self, data: i32) {
        self.data += data;
    }
}

//  To be able to pass the structure around, I put it in a RefCell for interior mutability and Rc
//  for the ref counting. For convenience I wrap everything in a new type.
#[derive(Clone)]
struct MyStateRef(Rc&lt;RefCell&lt;MyState&gt;&gt;);

impl MyStateRef {
    fn new() -&gt; Self {
        MyStateRef(Rc::new(RefCell::new(MyState{data: 0})))
    }
}

fn main() {
    //  Create an instance of MyState wrapped in Rc and RefCell
    let state = MyStateRef::new();

    //  &quot;state&quot; could now be cloned and passed around. At some point I would like to modify the
    //  internal MyState by calling modify_state.
    state.0 [...???...] .modify_state(3);
}

I'm not able to make the last line in the main function work. I tried various combinations of borrow_mut, get_mut, ... but just failed. I think that there is some Deref and DerefMut involved that is confusing me, but I cannot wrap my head around it.

Can somebody make the the last line work and explain why it is working?

答案1

得分: 4

borrow_mut 是你的朋友:

state.0.borrow_mut().modify_state(3);
英文:

<a href="https://doc.rust-lang.org/std/cell/struct.RefCell.html#method.borrow_mut">borrow_mut</a> is your friend:

    state.0.borrow_mut().modify_state(3);

huangapple
  • 本文由 发表于 2023年5月6日 19:02:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/76188506.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定