<예제 코드_1>
enum List {
Cons(i32, Rc<List>),
Nil,
}
use List::{Cons, Nil};
use std::rc::Rc;
fn main() {
let a = Rc::new(Cons(5, Rc::new(Cons(10, Rc::new(Nil)))));
println!("count after creating a = {}", Rc::strong_count(&a));
let b = Cons(3, Rc::clone(&a));
println!("count after creating b = {}", Rc::strong_count(&a));
{
let c = Cons(4, Rc::clone(&a));
println!("count after creating c = {}", Rc::strong_count(&a));
}
println!("count after c goes out of scope = {}", Rc::strong_count(&a));
}
<참조 1> https://rinthel.github.io/rust-lang-book-ko/ch15-04-rc.html
<참조_2>
'Rust' 카테고리의 다른 글
[Rust] Circular Reference (순환 참조) 예제 (40일차) (0) | 2023.01.23 |
---|---|
[Rust] RefCell 예제 (39일차) (0) | 2023.01.22 |
[Rust] Ownership(소유권) 기초 예제 : &str (37일차) (0) | 2023.01.20 |
[Rust] Deref(역참조) 예제 (36일차) (0) | 2023.01.19 |
댓글