From 12641cba911fa43a81c996453c2b4757ea2bcc53 Mon Sep 17 00:00:00 2001 From: Rowan Torbitzky-Lane Date: Tue, 1 Apr 2025 11:30:25 -0500 Subject: [PATCH] part of ch15.02 done --- ch15/boxt/Cargo.lock | 7 +++++ ch15/boxt/Cargo.toml | 6 ++++ ch15/boxt/src/main.rs | 31 ++++++++++++++++++++ ch15/sect-deref/Cargo.lock | 7 +++++ ch15/sect-deref/Cargo.toml | 6 ++++ ch15/sect-deref/src/main.rs | 56 +++++++++++++++++++++++++++++++++++++ 6 files changed, 113 insertions(+) create mode 100644 ch15/boxt/Cargo.lock create mode 100644 ch15/boxt/Cargo.toml create mode 100644 ch15/boxt/src/main.rs create mode 100644 ch15/sect-deref/Cargo.lock create mode 100644 ch15/sect-deref/Cargo.toml create mode 100644 ch15/sect-deref/src/main.rs diff --git a/ch15/boxt/Cargo.lock b/ch15/boxt/Cargo.lock new file mode 100644 index 0000000..56fa3f5 --- /dev/null +++ b/ch15/boxt/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "boxt" +version = "0.1.0" diff --git a/ch15/boxt/Cargo.toml b/ch15/boxt/Cargo.toml new file mode 100644 index 0000000..e0af3d3 --- /dev/null +++ b/ch15/boxt/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "boxt" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/ch15/boxt/src/main.rs b/ch15/boxt/src/main.rs new file mode 100644 index 0000000..b4c92e0 --- /dev/null +++ b/ch15/boxt/src/main.rs @@ -0,0 +1,31 @@ +//! Most straight forward smart pointer is a box, `Box`. +//! Use boxes most often when: +//! 1) When have type whose size not known at compile time +//! and want to use value of said type in a context +//! that requires an exact size +//! 2) When have large amount of data and want to transfer +//! ownership but ensure data won't be copied +//! 3) When want to own a value and only care it implements +//! a particular trait rather than being of a specific type + +use crate::List::{Cons, Nil}; + +enum List { + // Because Box is a pointer, Rust knows how much + // space a Box needs. + Cons(i32, Box), + Nil, +} + +fn main() { + let b = Box::new(5); + println!("b = {b}"); + + // enabling recursive types with Boxes + // Recursive types an issue because Rust needs to know + // how much space a type takes up. Boxes have a known size + // so can enable recursive types by inserting a box in + // the recursive type definition. Basically Haskell. + // That's it, time to rewrite the ghc in Rust. + let list = Cons(1, Box::new(Cons(2, Box::new(Cons(3, Box::new(Nil)))))); +} diff --git a/ch15/sect-deref/Cargo.lock b/ch15/sect-deref/Cargo.lock new file mode 100644 index 0000000..93b5392 --- /dev/null +++ b/ch15/sect-deref/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-deref" +version = "0.1.0" diff --git a/ch15/sect-deref/Cargo.toml b/ch15/sect-deref/Cargo.toml new file mode 100644 index 0000000..5cf7888 --- /dev/null +++ b/ch15/sect-deref/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "sect-deref" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/ch15/sect-deref/src/main.rs b/ch15/sect-deref/src/main.rs new file mode 100644 index 0000000..9b28b81 --- /dev/null +++ b/ch15/sect-deref/src/main.rs @@ -0,0 +1,56 @@ +//! Implementing Deref allows customization of the * operator. + +use std::ops::Deref; + +// defining our own smart pointer +// for struct MyBox and its implementation +struct MyBox(T); + +impl MyBox { + fn new(x: T) -> MyBox { + MyBox(x) + } +} + +// treating a type like a reference by implementing the Deref trait +// impl section +impl Deref for MyBox { + type Target = T; + + fn deref(&self) -> &Self::Target { + // This is a tuple with one element inside of a box?? + &self.0 + } +} + +fn main() { + // following the pointer to the value + let x = 5; + let y = &x; + + // *y to follow the reference to the value in x. + assert_eq!(5, x); + assert_eq!(5, *y); + + // Using Box like a reference + let x = 5; + let y = Box::new(x); + + assert_eq!(5, x); + assert_eq!(5, *y); + + // defining our own smart pointer + let x = 5; + let y = MyBox::new(x); + + assert_eq!(5, x); + assert_eq!(5, *y); + + // Implicit deref coercions with functions and methods + + // 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 +}