/// Traverses the network of nodes and returns the input node
fn get_input(node: Rc<RefCell<Box<Node>>>) -> Rc<RefCell<Box<Node>>> {
match **node.borrow() { // here
Node::Input { .. } => Rc::clone(&node),
_ => { ... },
}
}我不明白为什么要建议这个编辑,为什么它会起作用。我认为最令人困惑的是有时会发生的隐含引用。
例如,.borrow()是RefCell的一个方法,但我被允许直接在Rc上调用它。此外,这也可以工作:**(*node).borrow()。
发布于 2020-02-20 20:28:49
let node = *(*((*node).borrow()))相当于:
let node = *node; // Rc implements Deref, node is now RefCell<Box<Node>>
let node = node.borrow(); // a RefCell method, node is now Ref<<'empty>, Box<Node>>
let node = *node; // Ref implements Deref, node is now Box<Node>
let node = *node; // Box implements Deref, node is now Node显式形式的*(*((*node).borrow()))等同于隐式的**node.borrow(),因为(操作符优先级和) Deref强制,因此Rc会自动解除引用,以便.borrow()的调用应用于它所指向的内容。
https://stackoverflow.com/questions/60306527
复制相似问题