How to use future::executors::block_on in playgound?

Hi, I've been trying out a minor example in Rust playgound and got stuck on

future::executors::block_on(future)

not being found. In fact it doesn't find futures::executors. Are these available in std?

I'm using Nightly channel. Thx!

P.S. I guess I am mightily confused between futures as they were outside of std - and which a lot of docs online describe - and futures as they are within std.. where's my await! macro? where's block_on? :sob:

1 Like

not being found. In fact it doesn't find futures::executors . Are these available in std ?

The situation as of futures-preview 0.3-alpha.* is that std defines the Future, but no adapters. Then futures (currently futures-preview-0.3.0-alpha.18) defines all the adapters and helper functions which use std's std::futrue::Future trait.

I guess I am mightily confused between futures as they were outside of std - and which a lot of docs online describe - and futures as they are within std .

Right now, there are indeed two futures.

The futures crate is at version 0.1, and it defines the old-style futures. Most crates dealing with futures use this version in their current "released" versions, since the other kind is very new.

The second kind of future is std::future::Future, which was recently stabilized. It is accompanied by combinators and adapters from the futures-preview crate vesion 0.3-alpha.18. Git versions of tokio, hyper, etc. use this kind of future.

The futures-preview crate provides compatibility shims if you want to work with both kinds of futures - there's documentation at futures::compat - Rust. For example, you can import the Compat01As03 trait, and then use fut.compat() to turn a futures-0.1 future into a std::future::Future future.

where's my await! macro?

This has been removed in favor of the .await syntax! More info on that decision here: A final proposal for await syntax

where's block_on ?

For std / futures-preview 0.3 futures, it's located at futures::executor::block_on - Rust.


Hope that helps! Let me know if you have any more questions about this.

2 Likes

So it seems Plaground is pulling in 0.1 futures, not 0.3? Because the following

#![feature(async_await)]

async fn haha() {
    println!("hello from haha");
}

fn main() {
    let future = haha();
    println!("hello from main");
    futures::executor::spawn(future).wait_future();
}

fails with

the method `wait_future` exists but the following trait bounds were not satisfied:
           `impl std::future::Future : futures::future::Future`

There's several factors which I think are preventing this from working:

  • futures 0.3 is currently only available via the futures-preview crate, to avoid breaking the existing ecosystem.
  • The playground only pulls in the top 100 crates (ranked by all-time downloads) and crates that are used in the Rust Cookbook. I'm not sure if futures-preview meets this criteria.
  • Even if it does, both futures and futures_preview use the same crate root path (use futures) - if they were both available, how would the playground know which one you meant to import?

I think your best bet is to just use a local project, at least until 0.3 is released in the main futures package.

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.