4

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.

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
zino
  • 1,222
  • 2
  • 17
  • 47
  • It's hard to answer your question because it doesn't include a [MRE]. We can't tell what crates (and their versions), types, traits, fields, etc. are present in the code. It would make it easier for us to help you if you try to reproduce your error on the [Rust Playground](https://play.rust-lang.org) if possible, otherwise in a brand new Cargo project, then [edit] your question to include the additional info. There are [Rust-specific MRE tips](//stackoverflow.com/tags/rust/info) you can use to reduce your original code for posting here. Thanks! – Shepmaster Sep 28 '20 at 13:11
  • Please [edit] your question and paste the exact and entire error that you're getting — that will help us to understand what the problem is so we can help best. Sometimes trying to interpret an error message is tricky and it's actually a different part of the error message that's important. Please use the message from running the compiler directly, not the message produced by an IDE, which might be trying to interpret the error for you. – Shepmaster Sep 28 '20 at 13:12
  • [Duplicates applied to your situation](https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=f51398bd8758cb484cae5c7b2721e033) (ish) – Shepmaster Sep 28 '20 at 13:17
  • @Shepmaster But wouldn't the type just be something like `Future>`? – zino Sep 28 '20 at 13:21
  • 1
    That's not a type (at least not one that you want to use or that has a known size). `Future` is a trait. – Shepmaster Sep 28 '20 at 13:23

0 Answers0