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..9d76c447cb 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 \ No newline at end of file