Skip to content

lifetime may not live long enough #37

@jennydaman

Description

@jennydaman

Hello,

I am wondering how to deal with this situation.

https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=9c9d1ddc0b3ece4b4bb40280c0fda6ca

use async_recursion::async_recursion;

#[tokio::main]
async fn main() {
    count_down(5, None).await;
}

#[async_recursion]
async fn count_down(num: u32, foo: Option<&str>) {
    if num == 0 {
        return;
    }
    println!("{}, {:?}", num, foo);
    count_down(num - 1, foo);
}

Output:

error: lifetime may not live long enough
 --> src/main.rs:8:1
  |
8 | #[async_recursion]
  | ^^^^^^^^^^^^^^^^^^ returning this value requires that `'1` must outlive `'static`
9 | async fn count_down(num: u32, foo: Option<&str>) {
  |                                           - let's call the lifetime of this reference `'1`
  |
  = note: this error originates in the attribute macro `async_recursion` (in Nightly builds, run with -Z macro-backtrace for more info)
help: to declare that the trait object captures data from argument `foo`, you can add an explicit `'_` lifetime bound
  |
8 | #[async_recursion] + '_
  |                    ++++

The "help" given by rustc is invalid.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions