summaryrefslogtreecommitdiffstats
path: root/src/doc/book/listings/ch15-smart-pointers/listing-15-19
diff options
context:
space:
mode:
Diffstat (limited to 'src/doc/book/listings/ch15-smart-pointers/listing-15-19')
-rw-r--r--src/doc/book/listings/ch15-smart-pointers/listing-15-19/Cargo.lock6
-rw-r--r--src/doc/book/listings/ch15-smart-pointers/listing-15-19/Cargo.toml6
-rw-r--r--src/doc/book/listings/ch15-smart-pointers/listing-15-19/output.txt8
-rw-r--r--src/doc/book/listings/ch15-smart-pointers/listing-15-19/src/main.rs21
4 files changed, 41 insertions, 0 deletions
diff --git a/src/doc/book/listings/ch15-smart-pointers/listing-15-19/Cargo.lock b/src/doc/book/listings/ch15-smart-pointers/listing-15-19/Cargo.lock
new file mode 100644
index 000000000..a792c49aa
--- /dev/null
+++ b/src/doc/book/listings/ch15-smart-pointers/listing-15-19/Cargo.lock
@@ -0,0 +1,6 @@
+# This file is automatically @generated by Cargo.
+# It is not intended for manual editing.
+[[package]]
+name = "cons-list"
+version = "0.1.0"
+
diff --git a/src/doc/book/listings/ch15-smart-pointers/listing-15-19/Cargo.toml b/src/doc/book/listings/ch15-smart-pointers/listing-15-19/Cargo.toml
new file mode 100644
index 000000000..dce1515c3
--- /dev/null
+++ b/src/doc/book/listings/ch15-smart-pointers/listing-15-19/Cargo.toml
@@ -0,0 +1,6 @@
+[package]
+name = "cons-list"
+version = "0.1.0"
+edition = "2021"
+
+[dependencies]
diff --git a/src/doc/book/listings/ch15-smart-pointers/listing-15-19/output.txt b/src/doc/book/listings/ch15-smart-pointers/listing-15-19/output.txt
new file mode 100644
index 000000000..6a8cc8efe
--- /dev/null
+++ b/src/doc/book/listings/ch15-smart-pointers/listing-15-19/output.txt
@@ -0,0 +1,8 @@
+$ cargo run
+ Compiling cons-list v0.1.0 (file:///projects/cons-list)
+ Finished dev [unoptimized + debuginfo] target(s) in 0.45s
+ Running `target/debug/cons-list`
+count after creating a = 1
+count after creating b = 2
+count after creating c = 3
+count after c goes out of scope = 2
diff --git a/src/doc/book/listings/ch15-smart-pointers/listing-15-19/src/main.rs b/src/doc/book/listings/ch15-smart-pointers/listing-15-19/src/main.rs
new file mode 100644
index 000000000..1bd7bc533
--- /dev/null
+++ b/src/doc/book/listings/ch15-smart-pointers/listing-15-19/src/main.rs
@@ -0,0 +1,21 @@
+enum List {
+ Cons(i32, Rc<List>),
+ Nil,
+}
+
+use crate::List::{Cons, Nil};
+use std::rc::Rc;
+
+// ANCHOR: here
+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));
+}
+// ANCHOR_END: here