mirror of
https://github.com/gnu4cn/rust-lang-zh_CN.git
synced 2026-08-19 12:43:28 +08:00
Updated 'src/smart_pointers/ref_cycles'.
This commit is contained in:
@@ -1,24 +1,43 @@
|
||||
#[derive(Debug)]
|
||||
enum List {
|
||||
Cons(Rc<RefCell<i32>>, Rc<List>),
|
||||
Nil,
|
||||
}
|
||||
|
||||
use crate::List::{Cons, Nil};
|
||||
use std::cell::RefCell;
|
||||
use std::rc::Rc;
|
||||
|
||||
fn main() {
|
||||
let value = Rc::new(RefCell::new(5));
|
||||
|
||||
let a = Rc::new(Cons(Rc::clone(&value), Rc::new(Nil)));
|
||||
|
||||
let b = Cons(Rc::new(RefCell::new(3)), Rc::clone(&a));
|
||||
let c = Cons(Rc::new(RefCell::new(4)), Rc::clone(&a));
|
||||
|
||||
*value.borrow_mut() += 10;
|
||||
|
||||
println!("a 随后 = {a:?}");
|
||||
println!("b 随后 = {b:?}");
|
||||
println!("c 随后 = {c:?}");
|
||||
#[derive(Debug)]
|
||||
enum List {
|
||||
Cons(i32, RefCell<Rc<List>>),
|
||||
Nil,
|
||||
}
|
||||
|
||||
impl List {
|
||||
fn tail(&self) -> Option<&RefCell<Rc<List>>> {
|
||||
match self {
|
||||
Cons(_, item) => Some(item),
|
||||
Nil => None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
fn main() {
|
||||
let a = Rc::new(Cons(5, RefCell::new(Rc::new(Nil))));
|
||||
|
||||
println! ("a 的初始 rc 计数 = {}", Rc::strong_count(&a));
|
||||
println! ("a 的下一条目 = {:?}", a.tail().unwrap());
|
||||
|
||||
let b = Rc::new(Cons(10, RefCell::new(Rc::clone(&a))));
|
||||
|
||||
println! ("b 创建后 a 的 rc 计数 = {}", Rc::strong_count(&a));
|
||||
println! ("b 的初始 rc 计数 = {}", Rc::strong_count(&b));
|
||||
println! ("b 的下一条目 = {:?}", b.tail().unwrap());
|
||||
|
||||
if let Some(link) = a.tail() {
|
||||
*link.borrow_mut() = Rc::clone(&b);
|
||||
}
|
||||
|
||||
println! ("修改 a 之后 b 的 rc 计数 = {}", Rc::strong_count(&b));
|
||||
println! ("修改 a 之后 a 的 rc 计数 = {}", Rc::strong_count(&a));
|
||||
|
||||
// 取消注释下一行,就可以看到我们有个循环;
|
||||
// 他将导致栈溢出。
|
||||
println! ("a 的下一条目 = {:?}", a.tail());
|
||||
}
|
||||
|
||||
@@ -1,8 +0,0 @@
|
||||
[package]
|
||||
name = "ref_cycle_demo"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
@@ -1,52 +0,0 @@
|
||||
use std::cell::RefCell;
|
||||
use std::rc::Rc;
|
||||
use crate::List::{Cons, Nil};
|
||||
|
||||
#[derive(Debug)]
|
||||
enum List {
|
||||
Cons(i32, RefCell<Rc<List>>),
|
||||
Nil,
|
||||
}
|
||||
|
||||
impl List {
|
||||
fn tail(&self) -> Option<&RefCell<Rc<List>>> {
|
||||
match self {
|
||||
Cons(_, item) => Some(item),
|
||||
Nil => None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let a = Rc::new(
|
||||
Cons(
|
||||
5,
|
||||
RefCell::new(Rc::new(Nil))
|
||||
)
|
||||
);
|
||||
|
||||
println! ("a 的初始 rc 计数 = {}", Rc::strong_count(&a));
|
||||
println! ("a 的下一条目 = {:?}", a.tail());
|
||||
|
||||
let b = Rc::new(
|
||||
Cons(
|
||||
10,
|
||||
RefCell::new(Rc::clone(&a))
|
||||
)
|
||||
);
|
||||
|
||||
println! ("创建 b 后 a 的 rc 计数 = {}", Rc::strong_count(&a));
|
||||
println! ("b 的初始 rc 计数 = {}", Rc::strong_count(&b));
|
||||
println! ("b 的下一条目 = {:?}", b.tail());
|
||||
|
||||
if let Some(link) = a.tail() {
|
||||
*link.borrow_mut() = Rc::clone(&b);
|
||||
}
|
||||
|
||||
println! ("在修改 a 之后 b 的 rc 计数 = {}", Rc::strong_count(&b));
|
||||
println! ("在修改 a 之后 a 的 rc 计数 = {}", Rc::strong_count(&a));
|
||||
|
||||
// 取消下面这行注释,就可以看到这里有着循环引用;
|
||||
// 他将溢出堆栈(it will overflow the stack)
|
||||
// println! ("a 的下一条目 = {:?}", a.tail());
|
||||
}
|
||||
@@ -15,10 +15,9 @@ fn main() {
|
||||
children: RefCell::new(vec! []),
|
||||
});
|
||||
|
||||
println! (
|
||||
"叶子节点的强引用计数:{},弱引用计数:{}\n",
|
||||
println! ("leaf 的强引用计数 = {},弱引用计数 = {}",
|
||||
Rc::strong_count(&leaf),
|
||||
Rc::weak_count(&leaf),
|
||||
Rc::weak_count(&leaf)
|
||||
);
|
||||
|
||||
{
|
||||
@@ -30,22 +29,26 @@ fn main() {
|
||||
|
||||
*leaf.parent.borrow_mut() = Rc::downgrade(&branch);
|
||||
|
||||
println! (
|
||||
"枝干节点的强引用计数:{},弱引用计数:{}\n",
|
||||
println! ("branch 的强引用计数 = {},弱引用计数 = {}",
|
||||
Rc::strong_count(&branch),
|
||||
Rc::weak_count(&branch),
|
||||
);
|
||||
println! (
|
||||
"叶子节点的强引用计数:{},弱引用计数:{}\n",
|
||||
Rc::strong_count(&leaf),
|
||||
Rc::weak_count(&leaf),
|
||||
Rc::weak_count(&branch)
|
||||
);
|
||||
|
||||
println! ("leaf 的强引用计数 = {},弱引用计数 = {}",
|
||||
Rc::strong_count(&leaf),
|
||||
Rc::weak_count(&leaf)
|
||||
);
|
||||
}
|
||||
println! ("叶子节点的父节点 = {:?}\n", leaf.parent.borrow().upgrade());
|
||||
println! (
|
||||
"叶子节点的强引用计数:{},弱引用计数:{}\n",
|
||||
|
||||
println! ("leaf 的父节点 = {:?}", leaf.parent.borrow().upgrade());
|
||||
|
||||
println! ("leaf 的强引用计数 = {},弱引用计数 = {}",
|
||||
Rc::strong_count(&leaf),
|
||||
Rc::weak_count(&leaf),
|
||||
Rc::weak_count(&leaf)
|
||||
);
|
||||
// println! ("
|
||||
// leaf rc = {}
|
||||
// branch rc(strong) = {}
|
||||
// branch rc(weak) = {}"
|
||||
// , Rc::strong_count(&leaf), Rc::strong_count(&branch), Rc::weak_count(&branch));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user