diff --git a/content/lessons/06_closures_iterators/closures_fun.rs b/content/lessons/06_closures_iterators/closures_fun.rs index 46a8748..9f2ae91 100644 --- a/content/lessons/06_closures_iterators/closures_fun.rs +++ b/content/lessons/06_closures_iterators/closures_fun.rs @@ -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. diff --git a/content/lessons/06_closures_iterators/index.md b/content/lessons/06_closures_iterators/index.md index 6e55f05..df6b32d 100644 --- a/content/lessons/06_closures_iterators/index.md +++ b/content/lessons/06_closures_iterators/index.md @@ -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).