Hard
Two Rc<Node>s storing an Rc to each other
a and b are wired into a cycle via Rc. What prints, and what happens to their memory when main returns?
main.rs
use std::cell::RefCell;
use std::rc::Rc;
struct Node {
value: i32,
next: RefCell<Option<Rc<Node>>>,
prev: RefCell<Option<Rc<Node>>>,
}
impl Drop for Node {
fn drop(&mut self) {
println!("Dropping Node {}", self.value);
}
}
fn main() {
let a = Rc::new(Node { value: 1, next: RefCell::new(None), prev: RefCell::new(None) });
let b = Rc::new(Node { value: 2, next: RefCell::new(None), prev: RefCell::new(None) });
*a.next.borrow_mut() = Some(Rc::clone(&b));
*b.prev.borrow_mut() = Some(Rc::clone(&a));
println!("a count = {}, b count = {}", Rc::strong_count(&a), Rc::strong_count(&b));
}26 of 53 in Rust