Skip to content

Commit 7d298d1

Browse files
committed
Fix examples for returning closures.
1 parent d8a9570 commit 7d298d1

File tree

1 file changed

+24
-26
lines changed

1 file changed

+24
-26
lines changed

src/doc/trpl/closures.md

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -324,37 +324,34 @@ first, it may seem strange, but we’ll figure it out. Here’s how you’d prob
324324
try to return a closure from a function:
325325

326326
```rust,ignore
327-
fn factory() -> (Fn(i32) -> Vec<i32>) {
328-
let vec = vec![1, 2, 3];
327+
fn factory() -> (Fn(i32) -> i32) {
328+
let num = 5;
329329
330-
|n| vec.push(n)
330+
|x| x + num
331331
}
332332
333333
let f = factory();
334334
335-
let answer = f(4);
336-
assert_eq!(vec![1, 2, 3, 4], answer);
335+
let answer = f(1);
336+
assert_eq!(6, answer);
337337
```
338338

339339
This gives us these long, related errors:
340340

341341
```text
342342
error: the trait `core::marker::Sized` is not implemented for the type
343-
`core::ops::Fn(i32) -> collections::vec::Vec<i32>` [E0277]
344-
f = factory();
345-
^
346-
note: `core::ops::Fn(i32) -> collections::vec::Vec<i32>` does not have a
347-
constant size known at compile-time
348-
f = factory();
349-
^
350-
error: the trait `core::marker::Sized` is not implemented for the type
351-
`core::ops::Fn(i32) -> collections::vec::Vec<i32>` [E0277]
352-
factory() -> (Fn(i32) -> Vec<i32>) {
353-
^~~~~~~~~~~~~~~~~~~~~
354-
note: `core::ops::Fn(i32) -> collections::vec::Vec<i32>` does not have a constant size known at compile-time
355-
factory() -> (Fn(i32) -> Vec<i32>) {
356-
^~~~~~~~~~~~~~~~~~~~~
357-
343+
`core::ops::Fn(i32) -> i32` [E0277]
344+
fn factory() -> (Fn(i32) -> i32) {
345+
^~~~~~~~~~~~~~~~
346+
note: `core::ops::Fn(i32) -> i32` does not have a constant size known at compile-time
347+
fn factory() -> (Fn(i32) -> i32) {
348+
^~~~~~~~~~~~~~~~
349+
error: the trait `core::marker::Sized` is not implemented for the type `core::ops::Fn(i32) -> i32` [E0277]
350+
let f = factory();
351+
^
352+
note: `core::ops::Fn(i32) -> i32` does not have a constant size known at compile-time
353+
let f = factory();
354+
^
358355
```
359356

360357
In order to return something from a function, Rust needs to know what
@@ -364,16 +361,16 @@ way to give something a size is to take a reference to it, as references
364361
have a known size. So we’d write this:
365362

366363
```rust,ignore
367-
fn factory() -> &(Fn(i32) -> Vec<i32>) {
368-
let vec = vec![1, 2, 3];
364+
fn factory() -> &(Fn(i32) -> i32) {
365+
let num = 5;
369366
370-
|n| vec.push(n)
367+
|x| x + num
371368
}
372369
373370
let f = factory();
374371
375-
let answer = f(4);
376-
assert_eq!(vec![1, 2, 3, 4], answer);
372+
let answer = f(1);
373+
assert_eq!(6, answer);
377374
```
378375

379376
But we get another error:
@@ -448,7 +445,8 @@ assert_eq!(6, answer);
448445
We use a trait object, by `Box`ing up the `Fn`. There’s just one last problem:
449446

450447
```text
451-
error: `num` does not live long enough
448+
error: closure may outlive the current function, but it borrows `num`,
449+
which is owned by the current function [E0373]
452450
Box::new(|x| x + num)
453451
^~~~~~~~~~~
454452
```

0 commit comments

Comments
 (0)