mirror of https://github.com/KaiserY/trpl-zh-cn
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
34 lines
751 B
34 lines
751 B
// ANCHOR: here
|
|
use std::cell::RefCell;
|
|
use std::rc::{Rc, Weak};
|
|
|
|
#[derive(Debug)]
|
|
struct Node {
|
|
value: i32,
|
|
parent: RefCell<Weak<Node>>,
|
|
children: RefCell<Vec<Rc<Node>>>,
|
|
}
|
|
// ANCHOR_END: here
|
|
|
|
// ANCHOR: there
|
|
fn main() {
|
|
let leaf = Rc::new(Node {
|
|
value: 3,
|
|
parent: RefCell::new(Weak::new()),
|
|
children: RefCell::new(vec![]),
|
|
});
|
|
|
|
println!("leaf parent = {:?}", leaf.parent.borrow().upgrade());
|
|
|
|
let branch = Rc::new(Node {
|
|
value: 5,
|
|
parent: RefCell::new(Weak::new()),
|
|
children: RefCell::new(vec![Rc::clone(&leaf)]),
|
|
});
|
|
|
|
*leaf.parent.borrow_mut() = Rc::downgrade(&branch);
|
|
|
|
println!("leaf parent = {:?}", leaf.parent.borrow().upgrade());
|
|
}
|
|
// ANCHOR_END: there
|