-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Ch. 20: show both
impl Fn
and Box<dyn Fn>
The existing text here has a long historical record; it predates `impl` or `dyn` existing at all, much less the version we landed on in Rust 2018. Update it to suggest defaulting to `impl Fn`, with trait objects `Box<dyn Fn>` the fallback. Fixes #1514 Fixes #3272
- Loading branch information
1 parent
bb786c8
commit 8bad93e
Showing
5 changed files
with
31 additions
and
39 deletions.
There are no files selected for viewing
20 changes: 0 additions & 20 deletions
20
listings/ch20-advanced-features/no-listing-18-returns-closure/output.txt
This file was deleted.
Oops, something went wrong.
2 changes: 1 addition & 1 deletion
2
listings/ch20-advanced-features/no-listing-18-returns-closure/src/lib.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
fn returns_closure() -> dyn Fn(i32) -> i32 { | ||
fn returns_closure() -> impl Fn(i32) -> i32 { | ||
|x| x + 1 | ||
} |
3 changes: 0 additions & 3 deletions
3
listings/ch20-advanced-features/no-listing-19-returns-closure-trait-object/src/lib.rs
This file was deleted.
Oops, something went wrong.
15 changes: 15 additions & 0 deletions
15
listings/ch20-advanced-features/no-listing-19-returns-closure-trait-object/src/main.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
fn main() { | ||
let handlers = vec![returns_closure(), returns_initialized_closure(123)]; | ||
for handler in handlers { | ||
let output = handler(5); | ||
println!("{output}"); | ||
} | ||
} | ||
|
||
fn returns_closure() -> Box<dyn Fn(i32) -> i32> { | ||
Box::new(|x| x + 1) | ||
} | ||
|
||
fn returns_initialized_closure(init: i32) -> Box<dyn Fn(i32) -> i32> { | ||
Box::new(move |x| x + init) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters