Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't infer type after collecting an iterator of futures #61991

ibaryshnikov opened this issue Jun 20, 2019 · 2 comments


None yet
5 participants
Copy link

commented Jun 20, 2019

This code fails to compile:


use futures::future::join_all;

async fn do_work(n: u32) -> u32 {
    n + 1

async fn main() {
    let data = vec![1, 2, 3];
    let futures_to_wait = data
        .map(|value: &u32| do_work(*value))
    let results = join_all(futures_to_wait).await;
    println!("all work is done, results are {:?}", results);

Error message:

error[E0698]: type inside `async` object must be known in this context
  --> src/
16 |     let results = join_all(futures_to_wait).await;
   |                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ cannot infer type
note: the type is part of the `async` object because of this `await`
  --> src/
16 |     let results = join_all(futures_to_wait).await;
   |                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: aborting due to previous error

For more information about this error, try `rustc --explain E0698`.

More details:

rustc --version
rustc 1.37.0-nightly (2fe7b3383 2019-06-19)
edition = "2018"

runtime = "0.3.0-alpha.5"
futures-preview = "0.3.0-alpha.16"

Annotating variable type fixes the error: let futures_to_wait: Vec<_> = ...

@Nemo157 kindly created a playground example with similar error (though a bit different, mentioning generators instead of async objects: type inside generator must be known in this context)


This comment has been minimized.

Copy link

commented Jun 20, 2019

For context join_all has the signature:

pub fn join_all<I>(i: I) -> JoinAll<<I as IntoIterator>::Item> where
    I: IntoIterator,
    <I as IntoIterator>::Item: Future, 

so doesn't provide enough constraints to allow inferring a type for collect. It seems bad that the inference failure is pointing to the join_all(...).await expression instead of the futures_to_await variable/collect call where the initial failure is.


This comment has been minimized.

Copy link

commented Jun 24, 2019

This isn't a language issue, nor an inference bug-- there's simply not enough information here, similar to if a call to collect occurred without specifying Vec.

@cramertj cramertj closed this Jun 24, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.