Cannot infer lifetime for async method

Viewed 267

Consider the following code:

struct Base{
    value: u32
}

impl Base{
    async fn create_test_async(&self, some_string: &str) -> Option<Test>{ //error
        None
    }
    
    fn create_test(&self, some_string: &str) -> Option<Test>{ //ok
        None
    }
}

struct Test<'a>{
    value: &'a u32
}

Playground

The async fn create_test_async fails to compile with the error:

error[E0726]: implicit elided lifetime not allowed here
 --> src/lib.rs:6:68
  |
6 |     async fn create_test_async(&self, some_string: &str) -> Option<Test>{
  |                                                                    ^^^^- help: indicate the anonymous lifetime: `<'_>`

Surely it can be fixed with specifying the lifetime parameter as

async fn create_test_async<'a>(&'a self, some_string: &str) -> Option<Test<'a>>{
    None
}

QUESTION: Why is it required to specify lifetime parameter explicitly for async methods? What stops rust from inferring it?

0 Answers
Related