started ch15.04
This commit is contained in:
parent
12641cba91
commit
6597cebeba
@ -23,6 +23,10 @@ impl<T> Deref for MyBox<T> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn hello(name: &str) {
|
||||||
|
println!("Hello, {name}!");
|
||||||
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
// following the pointer to the value
|
// following the pointer to the value
|
||||||
let x = 5;
|
let x = 5;
|
||||||
@ -51,6 +55,17 @@ fn main() {
|
|||||||
// deref coercion converts a reference to a type that implements
|
// deref coercion converts a reference to a type that implements
|
||||||
// Deref trait into a reference to another type.
|
// Deref trait into a reference to another type.
|
||||||
|
|
||||||
// stopped here:
|
let m = MyBox::new(String::from("Rust"));
|
||||||
// https://rust-book.cs.brown.edu/ch15-02-deref.html#implicit-deref-coercions-with-functions-and-methods
|
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<Target=U>`
|
||||||
|
// 2) From &mut T to &mut U when `T: DerefMut<Target=U>`
|
||||||
|
// 3) From &mut T to &U when `T: Deref<Target=U>`
|
||||||
|
//
|
||||||
|
// Rust will never coerce an immutable reference into a
|
||||||
|
// mutable reference.
|
||||||
}
|
}
|
||||||
|
7
ch15/sect-drop/Cargo.lock
generated
Normal file
7
ch15/sect-drop/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-drop"
|
||||||
|
version = "0.1.0"
|
6
ch15/sect-drop/Cargo.toml
Normal file
6
ch15/sect-drop/Cargo.toml
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
[package]
|
||||||
|
name = "sect-drop"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
[dependencies]
|
34
ch15/sect-drop/src/main.rs
Normal file
34
ch15/sect-drop/src/main.rs
Normal file
@ -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.");
|
||||||
|
}
|
7
ch15/sect-rc/Cargo.lock
generated
Normal file
7
ch15/sect-rc/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-rc"
|
||||||
|
version = "0.1.0"
|
6
ch15/sect-rc/Cargo.toml
Normal file
6
ch15/sect-rc/Cargo.toml
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
[package]
|
||||||
|
name = "sect-rc"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
[dependencies]
|
6
ch15/sect-rc/src/main.rs
Normal file
6
ch15/sect-rc/src/main.rs
Normal file
@ -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!");
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user