From f700d0aadb997e38b27c04457e492cec3780bfef Mon Sep 17 00:00:00 2001 From: SomewhereOutInSpace Date: Sun, 12 Feb 2023 23:18:32 -0500 Subject: [PATCH] Update 19.5 to demonstrate using impl Trait to return closures in addition to boxed trait objects --- .../Cargo.lock | 6 ++++++ .../Cargo.toml | 6 ++++++ .../src/lib.rs | 3 +++ src/ch19-05-advanced-functions-and-closures.md | 17 ++++++++++++++--- 4 files changed, 29 insertions(+), 3 deletions(-) create mode 100644 listings/ch19-advanced-features/no-listing-23-returns-closure-impl-trait/Cargo.lock create mode 100644 listings/ch19-advanced-features/no-listing-23-returns-closure-impl-trait/Cargo.toml create mode 100644 listings/ch19-advanced-features/no-listing-23-returns-closure-impl-trait/src/lib.rs diff --git a/listings/ch19-advanced-features/no-listing-23-returns-closure-impl-trait/Cargo.lock b/listings/ch19-advanced-features/no-listing-23-returns-closure-impl-trait/Cargo.lock new file mode 100644 index 0000000000..b2327c755a --- /dev/null +++ b/listings/ch19-advanced-features/no-listing-23-returns-closure-impl-trait/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "functions-example" +version = "0.1.0" + diff --git a/listings/ch19-advanced-features/no-listing-23-returns-closure-impl-trait/Cargo.toml b/listings/ch19-advanced-features/no-listing-23-returns-closure-impl-trait/Cargo.toml new file mode 100644 index 0000000000..b196f35b55 --- /dev/null +++ b/listings/ch19-advanced-features/no-listing-23-returns-closure-impl-trait/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "functions-example" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/listings/ch19-advanced-features/no-listing-23-returns-closure-impl-trait/src/lib.rs b/listings/ch19-advanced-features/no-listing-23-returns-closure-impl-trait/src/lib.rs new file mode 100644 index 0000000000..7679cc7c0a --- /dev/null +++ b/listings/ch19-advanced-features/no-listing-23-returns-closure-impl-trait/src/lib.rs @@ -0,0 +1,3 @@ +fn returns_closure() -> impl Fn(i32) -> i32 { + |x| x + 1 +} diff --git a/src/ch19-05-advanced-functions-and-closures.md b/src/ch19-05-advanced-functions-and-closures.md index 69624f056e..f4576d8aa9 100644 --- a/src/ch19-05-advanced-functions-and-closures.md +++ b/src/ch19-05-advanced-functions-and-closures.md @@ -109,17 +109,26 @@ The compiler error is as follows: ``` The error references the `Sized` trait again! Rust doesn’t know how much space -it will need to store the closure. We saw a solution to this problem earlier. +it will need to store the closure. We saw some solutions to this problem earlier. + We can use a trait object: ```rust,noplayground {{#rustdoc_include ../listings/ch19-advanced-features/no-listing-19-returns-closure-trait-object/src/lib.rs}} ``` -This code will compile just fine. For more about trait objects, refer to the +(For more about trait objects, refer to the section [“Using Trait Objects That Allow for Values of Different Types”][using-trait-objects-that-allow-for-values-of-different-types] in Chapter 17. +ignore --> in Chapter 17.) + +Or we can use the [`impl Trait` return type syntax](returning-types-that-implement-traits): + +```rust,noplayground +{{#rustdoc_include ../listings/ch19-advanced-features/no-listing-23-returns-closure-impl-trait/src/lib.rs}} +``` + +Either of these snippets will compile just fine. Next, let’s look at macros! @@ -128,3 +137,5 @@ ch19-03-advanced-traits.html#advanced-traits [enum-values]: ch06-01-defining-an-enum.html#enum-values [using-trait-objects-that-allow-for-values-of-different-types]: ch17-02-trait-objects.html#using-trait-objects-that-allow-for-values-of-different-types +[returning-types-that-implement-traits]: +ch10-02-traits.html#returning-types-that-implement-traits