I am havin issues working with Rc and RefCell. when I call a function that exists in the Contained Struct, the compiler complains the method 'add_child' is not found.
When I fun the below code I get the error
x.add_edge(jfk); | ^^^^^^^^ method not found in `&mut Rc<RefCell<Node>>
Directory structure:
/graph
/node.rs
/mod.rs
main.rs
main.rs
use std::borrow::BorrowMut;
fn main() {
let dfw = Rc::new(RefCell::new(Node::new(1)));
let jfk = Rc::new(RefCell::new(Node::new(2)));
dfw.borrow_mut().add_child(jfk) // -> This method call causes the error
}
graph/node.rs
#[derive(Debug)]
pub struct Node {
pub id: u32,
pub children: Vec<Rc<RefCell<Node>>>,
}
impl Node {
pub fn add_child(&mut self, node: Rc<RefCell<Node>>) {
self.children.push(node);
}
}
The
main.rswas importingRemoving this line fixed it.