我有一个异步函数,我正在向它传递异步回调.回调函数将引用作为参数.
use core::future::Future;
async fn foo(bar: &u32) {}
async fn baz<F, Fut>(f: F)
where
F: FnOnce(&u32) -> Fut,
Fut: Future<Output = ()>,
{
let test: u32 = 42;
f(&test).await;
}
#[tokio::main]
async fn main() {
baz(foo).await;
}
如果我try 构建这个(playground),我会遇到以下错误:
error[E0308]: mismatched types
--> src/main.rs:16:5
|
16 | baz(foo).await;
| ^^^ lifetime mismatch
|
= note: expected associated type `<for<'_> fn(&u32) -> impl Future<Output = ()> {foo} as FnOnce<(&u32,)>>::Output`
found associated type `<for<'_> fn(&u32) -> impl Future<Output = ()> {foo} as FnOnce<(&u32,)>>::Output`
= note: the required lifetime does not necessarily outlive the empty lifetime
note: the lifetime requirement is introduced here
--> src/main.rs:7:24
|
7 | F: FnOnce(&u32) -> Fut,
| ^^^
我知道它对推荐人的生命周期不满意.然而,我不明白为什么.
- 我们borrow "测试"
- 我们执行回调f(即"foo")
- 在f完成之前,baz无法退出
所以,看起来借来的东西不可能比宣布测试的地方更长寿.
我错过了什么?