You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: content/lessons/07_smart_pointers/index.md
+11-5Lines changed: 11 additions & 5 deletions
Original file line number
Diff line number
Diff line change
@@ -20,7 +20,7 @@ The `Deref` trait allows us to overload the dereference (`*`) operator.
20
20
21
21
Apart from enabling access to the underlying value, implementing the `Deref` trait enables Rust to perform _deref coercion_ on the pointer - trying to remove as many levels of indirection as it can. What it means in practice is that we will be able to use it with any code working on plain references.
In general, there are three possible coercions that Rust can perform:
26
26
@@ -36,23 +36,29 @@ While the first two coercions are straightforward, the third one is possible bec
36
36
37
37
The `Box<T>` type is the most basic out of Rust's smart pointers, equivalent to C++'s `std::unique_ptr<T>`. It's a simple wrapper that makes sure the underlying memory gets allocated and freed properly.
The `Rc<T>` type is the equivalent of `std::shared_ptr<T>` from C++. There is one caveat to this though - because we're creating multiple references to the same object, those references have to be immutable in accordance with the ownership rules.
Good examples and explanation of the interior mutability pattern and runtime borrow checking can be found in the [book](https://doc.rust-lang.org/book/ch15-05-interior-mutability.html).
54
54
55
-
Alongisde the `RefCell<T>` type described above, there is an analogous [`Cell<T>`](https://doc.rust-lang.org/std/cell/struct.Cell.html) type that operates on values instead of references.
55
+
Alongside the `RefCell<T>` type described above, there is an analogous [`Cell<T>`](https://doc.rust-lang.org/std/cell/struct.Cell.html) type that operates on values instead of references.
56
+
57
+
# Convenient handling of `dyn` objects
58
+
59
+
In previous labs you learned about dynamic dispatch and its strengths. The largest drawback you noticed is most likely that they are _unsized_ (`!Sized`, where `!` being syntax signifying lack of trait implementation).
60
+
61
+
When storing an object on a heap, however, we can use it as a `dyn` object seamlessly.
0 commit comments