Skip to content

Commit

Permalink
06: include closures_fun sample
Browse files Browse the repository at this point in the history
  • Loading branch information
wprzytula committed Oct 31, 2024
1 parent c97ae14 commit bb4559d
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
1 change: 1 addition & 0 deletions content/lessons/06_closures_iterators/closures_fun.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ fn main() {
// So, for FnOnce, we need to be their owners to be able to call them,
// and we can't have a `dyn` object owned on stack.
// We will solve this problem soon with smart pointers (e.g., Box).
// This will give us `std::function` -like experience.
}

// Mutable reference to FnMut is required to be able to call it.
Expand Down
9 changes: 8 additions & 1 deletion content/lessons/06_closures_iterators/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,17 @@ Those traits and the `fn` type form a hierarchy: `fn` ⊆ `Fn` ⊆ `FnMut` ⊆

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

The following code sample demonstrates various ways to capture environment (borrowing or moving) and various
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:

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

### Closures as trait objects (in dynamic dispatch)

The following code sample shows how one can use closures as `dyn Trait` objects, bypassing the problem of them having anonymous types:

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


## Examples

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

0 comments on commit bb4559d

Please sign in to comment.