How to create a Vec of futures

1.6k Views Asked by At

I have this function:

async fn get_events(r: RequestBuilder) -> Result<Vec<RepoEvent>, reqwest::Error> {
    Ok(r.send().await?.json::<Vec<RepoEvent>>().await?)
}

I want to store a Vec of futures and await them all:

let mut events = vec![];
for i in 1..=x {
    let req: RequestBuilder = client.get(&format!("https://example.com/api?page={}", i));

    events.append(get_events(req));
}
try_join_all(events).await.unwrap();

I get a E0308: expected mutable reference, found opaque type.

What type should events be?

I can fix the issue by inferring the type:

let events = (1..=x).map(|i| {
    let req: RequestBuilder = client.get(&format!("https://example.com/api?page={}", i));

    get_events(req);
});

But I'd really like to know how to store futures in vectors.

0

There are 0 best solutions below