summaryrefslogtreecommitdiffstats
path: root/src/doc/book/listings/ch15-smart-pointers/listing-15-24
diff options
context:
space:
mode:
Diffstat (limited to 'src/doc/book/listings/ch15-smart-pointers/listing-15-24')
-rw-r--r--src/doc/book/listings/ch15-smart-pointers/listing-15-24/Cargo.lock6
-rw-r--r--src/doc/book/listings/ch15-smart-pointers/listing-15-24/Cargo.toml6
-rw-r--r--src/doc/book/listings/ch15-smart-pointers/listing-15-24/output.txt7
-rw-r--r--src/doc/book/listings/ch15-smart-pointers/listing-15-24/src/main.rs24
4 files changed, 43 insertions, 0 deletions
diff --git a/src/doc/book/listings/ch15-smart-pointers/listing-15-24/Cargo.lock b/src/doc/book/listings/ch15-smart-pointers/listing-15-24/Cargo.lock
new file mode 100644
index 000000000..a792c49aa
--- /dev/null
+++ b/src/doc/book/listings/ch15-smart-pointers/listing-15-24/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-24/Cargo.toml b/src/doc/book/listings/ch15-smart-pointers/listing-15-24/Cargo.toml
new file mode 100644
index 000000000..dce1515c3
--- /dev/null
+++ b/src/doc/book/listings/ch15-smart-pointers/listing-15-24/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-24/output.txt b/src/doc/book/listings/ch15-smart-pointers/listing-15-24/output.txt
new file mode 100644
index 000000000..21b3530d9
--- /dev/null
+++ b/src/doc/book/listings/ch15-smart-pointers/listing-15-24/output.txt
@@ -0,0 +1,7 @@
+$ cargo run
+ Compiling cons-list v0.1.0 (file:///projects/cons-list)
+ Finished dev [unoptimized + debuginfo] target(s) in 0.63s
+ Running `target/debug/cons-list`
+a after = Cons(RefCell { value: 15 }, Nil)
+b after = Cons(RefCell { value: 3 }, Cons(RefCell { value: 15 }, Nil))
+c after = Cons(RefCell { value: 4 }, Cons(RefCell { value: 15 }, Nil))
diff --git a/src/doc/book/listings/ch15-smart-pointers/listing-15-24/src/main.rs b/src/doc/book/listings/ch15-smart-pointers/listing-15-24/src/main.rs
new file mode 100644
index 000000000..e225bd862
--- /dev/null
+++ b/src/doc/book/listings/ch15-smart-pointers/listing-15-24/src/main.rs
@@ -0,0 +1,24 @@
+#[derive(Debug)]
+enum List {
+ Cons(Rc<RefCell<i32>>, Rc<List>),
+ Nil,
+}
+
+use crate::List::{Cons, Nil};
+use std::cell::RefCell;
+use std::rc::Rc;
+
+fn main() {
+ let value = Rc::new(RefCell::new(5));
+
+ let a = Rc::new(Cons(Rc::clone(&value), Rc::new(Nil)));
+
+ let b = Cons(Rc::new(RefCell::new(3)), Rc::clone(&a));
+ let c = Cons(Rc::new(RefCell::new(4)), Rc::clone(&a));
+
+ *value.borrow_mut() += 10;
+
+ println!("a after = {:?}", a);
+ println!("b after = {:?}", b);
+ println!("c after = {:?}", c);
+}