# Is it possible to annotate the lifetime of a future/async block?

**URL:** <https://users.rust-lang.org/t/is-it-possible-to-annotate-the-lifetime-of-a-future-async-block/59091>\
**Category:** uncategorized\
**Created:** [April 29, 2021, 4:53pm UTC](https://users.rust-lang.org/t/is-it-possible-to-annotate-the-lifetime-of-a-future-async-block/59091 "2021-04-29T16:53:39Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![bsmedberg](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/bsmedberg/32/12790_2.png) [@bsmedberg](https://users.rust-lang.org/u/bsmedberg)\
**Post date:** [April 29, 2021, 4:53pm UTC](https://users.rust-lang.org/t/is-it-possible-to-annotate-the-lifetime-of-a-future-async-block/59091/1 "2021-04-29T16:53:40Z")

</div>

I think I have a question about the "lifetime" of a Future returned by a function... although I'm not sure this is the right word for it, because "lifetime" usually refers to a reference. But what I mean is: I want to return a Future which holds a reference to some data, and I want the caller to guarantee/compiler to enforce that the return promise will be used or thrown away by that particular named lifetime.

Is there a way to explicitly annotate this kind of promise?

In addition, is there a way to annotate an async block so that the Future it produces has this explicit named lifetime instead of 'static?

Example code. This function works because the compiler knows that the future is immediately awaited:

```rust
async fn test_works() {
    let s = "ref";
    let promise = async {
        println!("Running async block: {}", s);
    };
    promise.await
}

```

I'd like to replicate this by having multiple functions and replacing the automatic lifetime inference with something explicit. e.g.:

```rust
fn test_broken_inner(s: &str) -> impl std::future::Future {
    async {
        println!("Running async block: {}", s);
    }
}

async fn test_broken_outer() {
    let s = "broken";
    test_broken_inner(s).await;
}

```

Is there any way to explicitly annotate the future like that? I tried some explicit lifetimes but they don't work. Is this a place where PhantomData would somehow help identify the lifetime of the Future? (can _traits_ express phantom lifetime relationships, or only concrete types?)

```rust
fn test_broken_inner<'a>(s: &'a str) -> impl std::future::Future<'a> {

```

---

<div class="post-metadata">

**Author:** ![Hyeonu](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/hyeonu/32/7970_2.png) [@Hyeonu](https://users.rust-lang.org/u/Hyeonu)\
**Post date:** [April 29, 2021, 5:35pm UTC](https://users.rust-lang.org/t/is-it-possible-to-annotate-the-lifetime-of-a-future-async-block/59091/2 "2021-04-29T17:35:25Z")

</div>

```rust
fn test_broken_inner<'a>(
    s: &'a str,
) -> impl std::future::Future + 'a {

```

Note that this lifetime pattern is covered by the [lifetime elision rule](https://doc.rust-lang.org/reference/lifetime-elision.html).

---

<div class="post-metadata">

**Author:** ![bsmedberg](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/bsmedberg/32/12790_2.png) [@bsmedberg](https://users.rust-lang.org/u/bsmedberg)\
**Post date:** [April 29, 2021, 8:01pm UTC](https://users.rust-lang.org/t/is-it-possible-to-annotate-the-lifetime-of-a-future-async-block/59091/3 "2021-04-29T20:01:09Z")

</div>

Your comment is true for standard reference lifetimes, but I'm not sure how it's relevant to my question here. The code example here doesn't compile because `Future` doesn't have a way to annotate its lifetime (at least that I've found).

---

<div class="post-metadata">

**Author:** ![steffahn](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/steffahn/32/47569_2.png) [@steffahn](https://users.rust-lang.org/u/steffahn)\
**Post date:** [April 29, 2021, 8:30pm UTC](https://users.rust-lang.org/t/is-it-possible-to-annotate-the-lifetime-of-a-future-async-block/59091/4 "2021-04-29T20:30:33Z")

</div>

If you use the function signature @Hyeonu posted, then all that's missing is an additional change to `async move`, and you're set:

```rust
fn test_broken_inner<'a>(s: &'a str) -> impl std::future::Future + 'a {
    async move {
        println!("Running async block: {}", s);
    }
}

async fn test_broken_outer() {
    let s = "broken";
    test_broken_inner(s).await;
}

```

(the names still say "broken" but the code above actually compiles)

Of course, one would usually use an equivalent `async fn` for this:

```rust
async fn test_broken_inner<'a>(s: &'a str) {
    println!("Running async block: {}", s);
}

```

Lifetime elision allows you to, equivalenty, write:

```rust
fn test_broken_inner(s: &str) -> impl std::future::Future + '_ {

```

or

```rust
async fn test_broken_inner(s: &str) {

```

respectively.

---

<div class="post-metadata">

**Author:** ![system](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/system/32/51372_2.png) [@system](https://users.rust-lang.org/u/system)\
**Post date:** [July 28, 2021, 8:31pm UTC](https://users.rust-lang.org/t/is-it-possible-to-annotate-the-lifetime-of-a-future-async-block/59091/5 "2021-07-28T20:31:13Z")

</div>

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.
