From 6597cebebae7e002421988ac7f019dea484d5530 Mon Sep 17 00:00:00 2001 From: Rowan Torbitzky-Lane Date: Tue, 1 Apr 2025 12:27:26 -0500 Subject: [PATCH] started ch15.04 --- ch15/sect-deref/src/main.rs | 19 +++++++++++++++++-- ch15/sect-drop/Cargo.lock | 7 +++++++ ch15/sect-drop/Cargo.toml | 6 ++++++ ch15/sect-drop/src/main.rs | 34 ++++++++++++++++++++++++++++++++++ ch15/sect-rc/Cargo.lock | 7 +++++++ ch15/sect-rc/Cargo.toml | 6 ++++++ ch15/sect-rc/src/main.rs | 6 ++++++ 7 files changed, 83 insertions(+), 2 deletions(-) create mode 100644 ch15/sect-drop/Cargo.lock create mode 100644 ch15/sect-drop/Cargo.toml create mode 100644 ch15/sect-drop/src/main.rs create mode 100644 ch15/sect-rc/Cargo.lock create mode 100644 ch15/sect-rc/Cargo.toml create mode 100644 ch15/sect-rc/src/main.rs diff --git a/ch15/sect-deref/src/main.rs b/ch15/sect-deref/src/main.rs index 9b28b81..b49c59e 100644 --- a/ch15/sect-deref/src/main.rs +++ b/ch15/sect-deref/src/main.rs @@ -23,6 +23,10 @@ impl Deref for MyBox { } } +fn hello(name: &str) { + println!("Hello, {name}!"); +} + fn main() { // following the pointer to the value let x = 5; @@ -51,6 +55,17 @@ fn main() { // deref coercion converts a reference to a type that implements // Deref trait into a reference to another type. - // stopped here: - // https://rust-book.cs.brown.edu/ch15-02-deref.html#implicit-deref-coercions-with-functions-and-methods + let m = MyBox::new(String::from("Rust")); + hello(&m); + + // How deref coercion interacts with mutability + // + // Rust does deref coercion when it finds types and + // trait implementations in three cases: + // 1) From &T to &U when `T: Deref` + // 2) From &mut T to &mut U when `T: DerefMut` + // 3) From &mut T to &U when `T: Deref` + // + // Rust will never coerce an immutable reference into a + // mutable reference. } diff --git a/ch15/sect-drop/Cargo.lock b/ch15/sect-drop/Cargo.lock new file mode 100644 index 0000000..05e0b61 --- /dev/null +++ b/ch15/sect-drop/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "sect-drop" +version = "0.1.0" diff --git a/ch15/sect-drop/Cargo.toml b/ch15/sect-drop/Cargo.toml new file mode 100644 index 0000000..7da3e3a --- /dev/null +++ b/ch15/sect-drop/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "sect-drop" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/ch15/sect-drop/src/main.rs b/ch15/sect-drop/src/main.rs new file mode 100644 index 0000000..1b152d9 --- /dev/null +++ b/ch15/sect-drop/src/main.rs @@ -0,0 +1,34 @@ +//! # Running Code on Cleanup with the Drop Trait +//! +//! Second trait important to the smart pointer is `Drop`, lets +//! customize what happens when value is about to go out of scope. + +use std::mem::drop; + +struct CustomSmartPointer { + data: String, +} + +impl Drop for CustomSmartPointer { + fn drop(&mut self) { + println!("Dropping CustomSmartPointer with data `{}`!", self.data); + } +} + +fn main() { + let c = CustomSmartPointer { + data: String::from("my stuff"), + }; + let d = CustomSmartPointer { + data: String::from("other stuff"), + }; + println!("CustomSmartPointers created."); + + let c = CustomSmartPointer { + data: String::from("some data"), + }; + println!("CustomSmartPointer created."); + // c.drop(); // not allowed to explictly call drop like this. + drop(c); + println!("CustomSmartPointer dropped before the end of main."); +} diff --git a/ch15/sect-rc/Cargo.lock b/ch15/sect-rc/Cargo.lock new file mode 100644 index 0000000..ca15201 --- /dev/null +++ b/ch15/sect-rc/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "sect-rc" +version = "0.1.0" diff --git a/ch15/sect-rc/Cargo.toml b/ch15/sect-rc/Cargo.toml new file mode 100644 index 0000000..7d8a9e6 --- /dev/null +++ b/ch15/sect-rc/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "sect-rc" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/ch15/sect-rc/src/main.rs b/ch15/sect-rc/src/main.rs new file mode 100644 index 0000000..d931b69 --- /dev/null +++ b/ch15/sect-rc/src/main.rs @@ -0,0 +1,6 @@ +//! Stopped here: +//! https://rust-book.cs.brown.edu/ch15-04-rc.html#rct-the-reference-counted-smart-pointer + +fn main() { + println!("Hello, world!"); +}