Rc<T> 和 RefCell<T> 和Weak<T>
1. Rc<T>
Rc<T>use std::rc::Rc;
fn main() {
let rc1 = Rc::new(5);
let rc2 = Rc::clone(&rc1); // 引用计数增加
println!("rc1: {}, rc2: {}", rc1, rc2);
println!("引用计数: {}", Rc::strong_count(&rc1)); // 输出: 2
}2. RefCell<T>
RefCell<T>3. Weak<T>
Weak<T>组合使用
总结
Last updated