2

考虑以下代码:

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
}

操场

async fn create_test_async编译失败,报错:

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: `<'_>`

当然可以通过将生命周期参数指定为来修复它

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

问题:为什么需要为async方法显式指定生命周期参数?是什么阻止 rust 推断它?

4

0 回答 0