mirror of
https://github.com/darkicewolf50/RustBrock.git
synced 2025-07-07 03:27:13 -06:00
finished ch15.4
This commit is contained in:
@ -9,7 +9,7 @@ References are indicated by the `&` symbol and borrow the value they point to.
|
||||
|
||||
They don't have any additional or special capabilities other than referring to data and have no overhead.
|
||||
|
||||
*Smart pointers* are data structures that act like a pointer but also have addtional metadata and capabilities.
|
||||
*Smart pointers* are data structures that act like a pointer but also have additional metadata and capabilities.
|
||||
|
||||
This idea of smart pointers is not unique to Rust.
|
||||
|
||||
@ -25,7 +25,7 @@ With the concept of ownership and borrowing in Rust it has an additional differe
|
||||
|
||||
While references only borrow data, in many cases, smart pointers *own* the data they point to.
|
||||
|
||||
Even though we havent called them smart pinters before we have already interacted with a few smart pointers in previous chapters.
|
||||
Even though we haven't called them smart pointers before we have already interacted with a few smart pointers in previous chapters.
|
||||
|
||||
This includes `String` and `Vec<T>`.
|
||||
|
||||
@ -33,7 +33,7 @@ Both of these types count as smart pointers because they own some memory and all
|
||||
|
||||
They both contain metadata and extra capabilities or guarantees.
|
||||
|
||||
As an example `String` stores its capacity as metadata and has the extra ability to enusre its data will always be valid UTF-8.
|
||||
As an example `String` stores its capacity as metadata and has the extra ability to ensure its data will always be valid UTF-8.
|
||||
|
||||
Smart pointers are usually implemented using structs.
|
||||
|
||||
@ -43,17 +43,17 @@ The `Deref` trait allows an instance of the smart pointer struct to behave like
|
||||
|
||||
The `Drop` trait allows you to customize the code that is run when an instance of the smart pointer goes out of scope. [Section Link Here](./Drop%20Trait.md)
|
||||
|
||||
We will go over both traits and demonstrate why they are improtant to smart pointers.
|
||||
We will go over both traits and demonstrate why they are important to smart pointers.
|
||||
|
||||
Given that the smart pointer pattern is a general design used often in Rust, we won't ocver every existing smart pointer.
|
||||
Given that the smart pointer pattern is a general design used often in Rust, we won't cover every existing smart pointer.
|
||||
|
||||
Many libraries have thier own smart pointers, and you can even write your own.
|
||||
Many libraries have their own smart pointers, and you can even write your own.
|
||||
|
||||
The ones we will cover are the most common smart pointers in the std library:
|
||||
- [`Box<T>` for allocating values on the heap](./Using%20Box%20on%20the%20Heap.md)
|
||||
- `Rc<T>`, a reference counting type that enables multiple ownership [Section Link Here](./Reference%20Counter%20Smart%20Pointer.md)
|
||||
- `Ref<T>` and `RefMut<T>` this is accessed through `RefCell<T>`, a type that enforces the borrowing rules at runtime instead of compile time
|
||||
- `Ref<T>` and `RefMut<T>` this is accessed through `RefCell<T>`, a type that enforces the borrowing rules at runtime instead of compile time [Section Link Here](./Ref%20Cell%20Mutability.md)
|
||||
|
||||
In additon we will also cover *interior mutability* patter where an immutable tpye exposes an API for mutating an interior value.
|
||||
In addition we will also cover *interior mutability* patter where an immutable type exposes an API for mutating an interior value.
|
||||
|
||||
As well discuss *reference cycles*; how they can leak memory and how to prevent them.
|
||||
|
Reference in New Issue
Block a user