Playing around with async/await I stumbled across this lifetime puzzle. I assume that predicate
takes a reference/value and produces static future - it has no reference to its arguments and captures no values. Instead I got complain about lifetime being too short.
Why it fails and where's the lifetime '2
?
use futures::future;
use std::future::Future;
fn validate<F, Fut>(_: F)
where
F: FnMut(&str) -> Fut,
Fut: Future<Output = bool>,
{
}
fn predicate(_: impl AsRef<str>) -> impl Future<Output = bool> {
future::ready(true)
}
fn main() {
validate(|x| predicate(x));
}
Error message:
error: lifetime may not live long enough
--> src\main.rs:16:18
|
16 | validate(|x| predicate(x));
| -- ^^^^^^^^^^^^ returning this value requires that `'1` must outlive `'2`
| ||
| |return type of closure is impl futures::Future
| has type `&'1 str`
error: aborting due to previous error