summaryrefslogtreecommitdiffstats
path: root/src/smartpointers
diff options
context:
space:
mode:
Diffstat (limited to 'src/smartpointers')
-rw-r--r--src/smartpointers/Cargo.toml6
-rw-r--r--src/smartpointers/src/main.rs16
2 files changed, 22 insertions, 0 deletions
diff --git a/src/smartpointers/Cargo.toml b/src/smartpointers/Cargo.toml
new file mode 100644
index 0000000..2a65854
--- /dev/null
+++ b/src/smartpointers/Cargo.toml
@@ -0,0 +1,6 @@
+[package]
+name = "smartpointers"
+version = "0.1.0"
+edition = "2024"
+
+[dependencies]
diff --git a/src/smartpointers/src/main.rs b/src/smartpointers/src/main.rs
new file mode 100644
index 0000000..594c5c1
--- /dev/null
+++ b/src/smartpointers/src/main.rs
@@ -0,0 +1,16 @@
+enum List {
+ Cons(i32, Rc<List>),
+ Nil,
+}
+
+use crate::List::{Cons, Nil};
+use std::rc::Rc;
+
+fn main() {
+ let a = Rc::new(Cons(5, Rc::new(Cons(10, Rc::new(Nil)))));
+ println!("a have {} references", Rc::strong_count(&a));
+ let b = Cons(3, Rc::clone(&a));
+ println!("a have {} references", Rc::strong_count(&a));
+ let c = Cons(4, Rc::clone(&a));
+ println!("a have {} references", Rc::strong_count(&a));
+}