why do i get a lifetime mismatch?

You have essentially the same problem as in here

To make it easier to understand I’ll rewrite/desugar some of your code. Let’s start with the predicate:

async fn filter(x: &Test) -> bool{
    x.0 >= 50

Is functionally equivalent to:

fn filter(x: &Test) -> impl Future<Output=bool> + '_{
    async {
        x.0 >= 50

Now it should be obvious that the lifetime of the returned Future is not 'static, but actually it depends on the lifetime of the parameter x: &Test.

This should make the error more understandable – your predicate is returning some future with some lifetime, which is not mentioned anywhere in the generic type definition – you can see that you don’t mention it when you define neither O, nor F:

    O: Future<Output = bool>,
    F: for Fn(&T) -> O,

The problem is that, currently the language does not provide any means to say that O‘s lifetime depends on F‘s lifetime. The only easy solution I know is to use the escape hatch – box the future, which will allow you to use the same lifetime in F and O. the downside is that, F will be heap allocated:

use futures::future::{join_all, Future};
use futures::future::BoxFuture;
use futures::FutureExt;

struct Test(usize);

async fn main() {
    let items = vec![Test(10), Test(100), Test(1000)];
    let items_filtered = filter_con(items, filter).await;

    println!("{:?}", items_filtered); // should print [100, 1000]

fn filter(x: &Test) -> BoxFuture<'_, bool>{
    async {
        x.0 >= 50

async fn filter_con<T, F>(arr: Vec<T>, predicate: F) -> Vec<T>
    F: for<'a> Fn(&'a T) -> BoxFuture<'a, bool>,
    join_all(arr.into_iter().map(|it| async{
        if predicate(&it).await {
        } else {
    .filter_map(|p| p)

Here is a link to the Rust Plauground

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top