Skip to content

Commit 11734e7

Browse files
committed
07: use new examples
1 parent 61f5667 commit 11734e7

File tree

1 file changed

+11
-5
lines changed
  • content/lessons/07_smart_pointers

1 file changed

+11
-5
lines changed

content/lessons/07_smart_pointers/index.md

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ The `Deref` trait allows us to overload the dereference (`*`) operator.
2020

2121
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.
2222

23-
{{ include_code_sample(path="lessons/old/2021L/10_smart_pointers/deref_coercion.rs", language="rust") }}
23+
{{ include_code_sample(path="lessons/07_smart_pointers/deref_coercion.rs", language="rust") }}
2424

2525
In general, there are three possible coercions that Rust can perform:
2626

@@ -36,23 +36,29 @@ While the first two coercions are straightforward, the third one is possible bec
3636

3737
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.
3838

39-
{{ include_code_sample(path="lessons/old/2021L/10_smart_pointers/box.rs", language="rust") }}
39+
{{ include_code_sample(path="lessons/07_smart_pointers/box.rs", language="rust") }}
4040

4141
# Reference counting
4242

4343
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.
4444

45-
{{ include_code_sample(path="lessons/old/2021L/10_smart_pointers/ref_count.rs", language="rust") }}
45+
{{ include_code_sample(path="lessons/07_smart_pointers/ref_count.rs", language="rust") }}
4646

4747
Rust also provides a non-owning pointer in the form of `Weak<T>` (equivalent to `std::weak_ptr<T>`) that can be obtained from an instance of `Rc<T>`.
4848

49-
{{ include_code_sample(path="lessons/old/2021L/10_smart_pointers/weak_ref.rs", language="rust") }}
49+
{{ include_code_sample(path="lessons/07_smart_pointers/weak_ref.rs", language="rust") }}
5050

5151
# Mutating the immutable
5252

5353
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).
5454

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.
5662

5763
# Obligatory reading
5864

0 commit comments

Comments
 (0)