Skip to content

Commit bb4559d

Browse files
committed
06: include closures_fun sample
1 parent c97ae14 commit bb4559d

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

content/lessons/06_closures_iterators/closures_fun.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ fn main() {
4040
// So, for FnOnce, we need to be their owners to be able to call them,
4141
// and we can't have a `dyn` object owned on stack.
4242
// We will solve this problem soon with smart pointers (e.g., Box).
43+
// This will give us `std::function` -like experience.
4344
}
4445

4546
// Mutable reference to FnMut is required to be able to call it.

content/lessons/06_closures_iterators/index.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,17 @@ Those traits and the `fn` type form a hierarchy: `fn` ⊆ `Fn` ⊆ `FnMut` ⊆
4646

4747
<!--> $$ fn \subseteq Fn \subseteq FnMut \subseteq FnOnce $$ -->
4848

49-
The following code sample demonstrates various ways to capture environment (borrowing or moving) and various
49+
The following code sample demonstrates various ways to capture environment (borrowing or moving) and various kinds of closures, based on what they do with their captures:
5050

5151
{{ include_code_sample(path="lessons/06_closures_iterators/closures_capturing.rs", language="rust") }}
5252

53+
### Closures as trait objects (in dynamic dispatch)
54+
55+
The following code sample shows how one can use closures as `dyn Trait` objects, bypassing the problem of them having anonymous types:
56+
57+
{{ include_code_sample(path="lessons/06_closures_iterators/closures_fun.rs", language="rust") }}
58+
59+
5360
## Examples
5461

5562
We'll go through the examples from [Rust by Example](https://doc.rust-lang.org/rust-by-example/fn/closures.html).

0 commit comments

Comments
 (0)