生锈"this parameter and the return type are declared with different lifetimes"



我正在使用Rust中的smol库。这个问题的其他答案都无济于事。

smol的Executor::spawn((声明如下:

pub fn spawn<T: Send + 'a>(&self, future: impl Future<Output = T> + Send + 'a) -> Task<T> {

现在我有了一个函数,想要递归地调用spawn,如下所示:

async fn start(executor: &Executor<'_>) {
let server_task = executor.spawn(async {
executor.spawn(async { println!("hello"); }).await;
});
}

但我得到了这个错误:

9  | async fn start(executor: &Executor<'_>) {
|                           ------------  -
|                           |
|                           this parameter and the return type are declared with different lifetimes...
...
18 |     let server_task = executor.spawn(async {
|                                ^^^^^ ...but data from `executor` is returned here

如何解决此错误?我很困惑。

use {
smol::{block_on, Executor},
std::sync::Arc,
};
// --
fn main() {
let ex = Arc::new(Executor::new());
block_on(ex.run(start(ex.clone())));
}
async fn start(executor: Arc<Executor<'_>>) {
let ex2 = executor.clone();
let server_task = executor.spawn(async move {
let t = ex2.spawn(async {
println!("hello");
});
t.await;
});
server_task.await;
}

相关内容

最新更新