pdiffley
pdiffley

Reputation: 713

Why do I get the error "no `block_on` in `executor`" when attempting to block on an async function?

I am following Rust's async/await primer but am having trouble running the hello world program shown below.

use futures::executor::block_on;

async fn hello_world() {
    println!("hello, world!");
}

fn main() {
    let future = hello_world(); // Nothing is printed
    block_on(future); // `future` is run and "hello, world!" is printed
}

I receive the error:

1 | use futures::executor::block_on;
  |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^ no `block_on` in `executor`

Is there an import required to use this function?

Upvotes: 0

Views: 1775

Answers (1)

pdiffley
pdiffley

Reputation: 713

As mentioned by Stargateur, futures = { version = "0.3", features = ["compat"] } needs to be added to your Cargo.toml dependencies.

The dependency is indicated at the top of the page following the hello world example.

Upvotes: 2

Related Questions