Function Currying that ultimately returns a Future

This is somewhat similar to @Yandros 's answer here

So you can name an "existential type" or a Type Alias to an impl Trait outside of the function, and then say that your function returns that type.

Like this (note - the async block required a move too):

type FutReturn = impl Future<Output = Option<Item>>;

fn find_by_name_cur(conn: String) -> impl FnOnce(String) -> FutReturn
{
    move |name: String| async move {
        println!(
            "fetching item by name: {} with connection: {}",
            name.clone(),
            conn.clone()
        );
        None
    }
}

Here's a working playground

1 Like