use std::rc::Rc;↵
↵
#[derive(Debug)]↵
struct Node {↵
value: i32,↵
children: Vec<Rc<Node>>,↵
}↵
↵
impl Node {↵
fn new(value: i32) -> Rc<Self> {↵
Rc::new(Node {↵
value,↵
children: Vec::new(),↵
})↵
}↵
↵
fn with_children(value: i32, children: Vec<Rc<Node>>) -> Rc<Self> {↵
Rc::new(Node { value, children })↵
}↵
}↵
↵
fn main() {↵
let leaf1 = Node::new(1);↵
let leaf2 = Node::new(2);↵
let leaf3 = Node::new(3);↵
↵
println!("leaf1 count: {}", Rc::strong_count(&leaf1));↵
↵
let branch = Node::with_children(10, vec![↵
Rc::clone(&leaf1),↵
Rc::clone(&leaf2),↵
]);↵
↵
println!("leaf1 count after branch: {}", Rc::strong_count(&leaf1));↵
↵
let root = Node::with_children(100, vec![↵
Rc::clone(&branch),↵
Rc::clone(&leaf3),↵
]);↵
↵
println!("branch count: {}", Rc::strong_count(&branch));↵
println!("Root: {:?}", root);↵
}