Implement Rc

main
sgoudham 3 years ago
parent 5ecaa30a75
commit dcbcc6dbd1
Signed by: hammy
GPG Key ID: 44E818FD5457EEA4

@ -1,4 +1,7 @@
use std::ops::Deref; use std::ops::Deref;
use std::rc::Rc;
use crate::List::{Cons, Nil};
struct MyBox<T>(T); struct MyBox<T>(T);
@ -26,6 +29,11 @@ impl Drop for CustomSmartPointer {
} }
} }
enum List {
Cons(i32, Rc<List>),
Nil,
}
fn hello(name: &str) { fn hello(name: &str) {
println!("Hello, {}!", name); println!("Hello, {}!", name);
} }
@ -49,4 +57,14 @@ fn main() {
println!("CustomSmartPointers created."); println!("CustomSmartPointers created.");
drop(c); drop(c);
println!("CustomSmartPointer dropped before the end of main."); println!("CustomSmartPointer dropped before the end of 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));
} }
Loading…
Cancel
Save