Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
fn returns_closure() -> impl Fn(i32) -> i32 {
|x| x + 1
fn returns_closure(init: i32) -> impl Fn(i32) -> i32 {
move |x| x + init
}
9 changes: 5 additions & 4 deletions src/ch20-04-advanced-functions-and-closures.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,15 +91,16 @@ compile to the same code, so use whichever style is clearer to you.
Closures are represented by traits, which means you can’t return closures
directly. In most cases where you might want to return a trait, you can instead
use the concrete type that implements the trait as the return value of the
function. However, you can’t do that with closures because they don’t have a
concrete type that is returnable; you’re not allowed to use the function
pointer `fn` as a return type, for example.
function. However, you can’t usually do that with closures because they don’t
usually have a concrete type that is returnable. You’re not allowed to use the
function pointer `fn` as a return type if the closure captures any values from
its scope, for example.

Instead, you will normally use the `impl Trait` syntax we learned about in
Chapter 10. You can return any function type, using `Fn`, `FnOnce` and `FnMut`.
For example, this code will work just fine:

```rust,ignore,does_not_compile
```rust
{{#rustdoc_include ../listings/ch20-advanced-features/no-listing-18-returns-closure/src/lib.rs}}
```

Expand Down