From f9dcd6d48d34d932abfd10e767803813c10b7cbe Mon Sep 17 00:00:00 2001 From: Timothy Warren Date: Tue, 5 Feb 2019 10:22:35 -0500 Subject: [PATCH] Add reference_counting example --- .idea/misc.xml | 1 + .idea/rust.iml | 5 +++ .idea/workspace.xml | 64 +++++++++++++++------------------- reference_counting/Cargo.toml | 7 ++++ reference_counting/src/main.rs | 19 ++++++++++ 5 files changed, 60 insertions(+), 36 deletions(-) create mode 100644 reference_counting/Cargo.toml create mode 100644 reference_counting/src/main.rs diff --git a/.idea/misc.xml b/.idea/misc.xml index 8ca6f05..465fbc7 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -25,6 +25,7 @@ + diff --git a/.idea/rust.iml b/.idea/rust.iml index bfb4a8b..73d9872 100644 --- a/.idea/rust.iml +++ b/.idea/rust.iml @@ -102,6 +102,10 @@ + + + + @@ -120,6 +124,7 @@ + diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 7405fa5..5369e4f 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -15,29 +15,14 @@ - + - - - - - - - - - - - - + + - - - - - @@ -107,6 +92,7 @@ @@ -140,12 +126,12 @@ - + - + @@ -179,7 +165,7 @@ - + @@ -207,7 +193,7 @@ - + + + - - + - @@ -731,13 +717,12 @@ - - + + - @@ -746,5 +731,12 @@ + + + + + + + \ No newline at end of file diff --git a/reference_counting/Cargo.toml b/reference_counting/Cargo.toml new file mode 100644 index 0000000..edcb9c1 --- /dev/null +++ b/reference_counting/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "reference_counting" +version = "0.1.0" +authors = ["Timothy Warren "] +edition = "2018" + +[dependencies] diff --git a/reference_counting/src/main.rs b/reference_counting/src/main.rs new file mode 100644 index 0000000..1ea8326 --- /dev/null +++ b/reference_counting/src/main.rs @@ -0,0 +1,19 @@ +enum List { + Cons(i32, Rc), + 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)); +}