Skip to content

Commit 3fc05f8

Browse files
authored
Merge pull request #1266 from andymac-2/closure-output-types
Chapter 9.2.5: impl FnOnce() works in 1.35
2 parents 1f5e21a + 23df515 commit 3fc05f8

File tree

1 file changed

+12
-7
lines changed

1 file changed

+12
-7
lines changed

src/fn/closures/output_parameters.md

+12-7
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,11 @@ output parameters should also be possible. However, anonymous
55
closure types are, by definition, unknown, so we have to use
66
`impl Trait` to return them.
77

8-
The valid traits for returns are slightly different than before:
8+
The valid traits for returning a closure are:
99

10-
* `Fn`: normal
11-
* `FnMut`: normal
12-
* `FnOnce`: There are some unusual things at play here, so the [`FnBox`][fnbox]
13-
type is currently needed, and is unstable. This is expected to change in
14-
the future.
10+
* `Fn`
11+
* `FnMut`
12+
* `FnOnce`
1513

1614
Beyond this, the `move` keyword must be used, which signals that all captures
1715
occur by value. This is required because any captures by reference would be
@@ -31,12 +29,20 @@ fn create_fnmut() -> impl FnMut() {
3129
move || println!("This is a: {}", text)
3230
}
3331
32+
fn create_fnonce() -> impl FnOnce() {
33+
let text = "FnOnce".to_owned();
34+
35+
move || println!("This is a: {}", text)
36+
}
37+
3438
fn main() {
3539
let fn_plain = create_fn();
3640
let mut fn_mut = create_fnmut();
41+
let fn_once = create_fnonce();
3742
3843
fn_plain();
3944
fn_mut();
45+
fn_once();
4046
}
4147
```
4248

@@ -46,6 +52,5 @@ fn main() {
4652

4753
[fn]: https://doc.rust-lang.org/std/ops/trait.Fn.html
4854
[fnmut]: https://doc.rust-lang.org/std/ops/trait.FnMut.html
49-
[fnbox]: https://doc.rust-lang.org/std/boxed/trait.FnBox.html
5055
[generics]: ../../generics.md
5156
[impltrait]: ../../trait/impl_trait.md

0 commit comments

Comments
 (0)