0
/// 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),
        _ => { ... },
    }
}

I'm confused as to why this edit was suggested and why it works. What I think is most confusing are the implicit dereferences that sometimes happen.

For example, .borrow() is a method of RefCell, yet I'm allowed to call it directly on an Rc. Moreover this works too: **(*node).borrow().

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
corazza
  • 31,222
  • 37
  • 115
  • 186
  • It looks like your question might be answered by the answers of [Meaning of the ampersand '&' and star '*' symbols in Rust](https://stackoverflow.com/q/36335342/155423); [What is the usage of the asterisk symbol in Rust?](https://stackoverflow.com/q/40531912/155423); [What are Rust's exact auto-dereferencing rules?](https://stackoverflow.com/q/28519997/155423). If not, please **[edit]** your question to explain the differences. Otherwise, we can mark this question as already answered. – Shepmaster Feb 19 '20 at 19:01
  • @Shepmaster my question was more about this specific example, but now I've figured it out see my answer – corazza Feb 20 '20 at 12:29

1 Answers1

1

let node = *(*((*node).borrow())) is equivalent to:

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

The explicit form *(*((*node).borrow())) is equivalent to the implicit **node.borrow() because (of operator precedence and) Deref coercion, whereby Rc is automatically dereferenced for the call of .borrow() to apply to what it's pointing to.

corazza
  • 31,222
  • 37
  • 115
  • 186