Skip to content

Commit

Permalink
Ch. 17: drop lifetime not required in 2024 Edition
Browse files Browse the repository at this point in the history
This makes for a nicely simpler introduction of this feature!
  • Loading branch information
chriskrycho committed Nov 25, 2024
1 parent 93d741a commit 2b371ee
Showing 1 changed file with 2 additions and 9 deletions.
11 changes: 2 additions & 9 deletions src/ch17-01-futures-and-syntax.md
Original file line number Diff line number Diff line change
Expand Up @@ -153,15 +153,15 @@ anonymous data type the compiler creates for that async block.

Thus, writing `async fn` is equivalent to writing a function which returns a
*future* of the return type. When the compiler sees a function definition such
as the `async fn page_title` in Listing 17-1, it’s equivalent to a non-async
as the `async fn page_title` in Listing 17-2, it’s equivalent to a non-async
function defined like this:

```rust
# extern crate trpl; // required for mdbook test
use std::future::Future;
use trpl::Html;

fn page_title(url: &str) -> impl Future<Output = Option<String>> + '_ {
fn page_title(url: &str) -> impl Future<Output = Option<String>> {
async move {
let text = trpl::get(url).await.text().await;
Html::parse(&text)
Expand All @@ -187,13 +187,6 @@ Let’s walk through each part of the transformed version:
* The new function body is an `async move` block because of how it uses the
`url` parameter. (We’ll talk about `async` vs. `async move` much more later
in the chapter.)
* The new version of the function has a kind of lifetime we haven’t seen before
in the output type: `'_`. Because the function returns a `Future` which refers
to a reference—in this case, the reference from the `url` parameter—we need to
tell Rust that we mean for that reference to be included. We don’t have to
name the lifetime here, because Rust is smart enough to know there is only one
reference which could be involved, but we *do* have to be explicit that the
resulting `Future` is bound by that lifetime.

Now we can call `page_title` in `main`. To start, we’ll just get the title
for a single page. In Listing 17-3, we follow the same pattern we used for
Expand Down

0 comments on commit 2b371ee

Please sign in to comment.