part of ch15.02 done
This commit is contained in:
parent
cb12f66182
commit
12641cba91
7
ch15/boxt/Cargo.lock
generated
Normal file
7
ch15/boxt/Cargo.lock
generated
Normal file
@ -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"
|
6
ch15/boxt/Cargo.toml
Normal file
6
ch15/boxt/Cargo.toml
Normal file
@ -0,0 +1,6 @@
|
||||
[package]
|
||||
name = "boxt"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
31
ch15/boxt/src/main.rs
Normal file
31
ch15/boxt/src/main.rs
Normal file
@ -0,0 +1,31 @@
|
||||
//! Most straight forward smart pointer is a box, `Box<t>`.
|
||||
//! 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<T> is a pointer, Rust knows how much
|
||||
// space a Box<T> needs.
|
||||
Cons(i32, Box<List>),
|
||||
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))))));
|
||||
}
|
7
ch15/sect-deref/Cargo.lock
generated
Normal file
7
ch15/sect-deref/Cargo.lock
generated
Normal file
@ -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"
|
6
ch15/sect-deref/Cargo.toml
Normal file
6
ch15/sect-deref/Cargo.toml
Normal file
@ -0,0 +1,6 @@
|
||||
[package]
|
||||
name = "sect-deref"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
56
ch15/sect-deref/src/main.rs
Normal file
56
ch15/sect-deref/src/main.rs
Normal file
@ -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>(T);
|
||||
|
||||
impl<T> MyBox<T> {
|
||||
fn new(x: T) -> MyBox<T> {
|
||||
MyBox(x)
|
||||
}
|
||||
}
|
||||
|
||||
// treating a type like a reference by implementing the Deref trait
|
||||
// impl section
|
||||
impl<T> Deref for MyBox<T> {
|
||||
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<T> 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
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user