Block_on error in rust1.39

block_on(learn_and_sing);
error[E0425]: cannot find function block_on in this scope
why?

Did you import block_on? Can you post a bit more code?

@CryZe Yes.

use std::futures::executor::block_on;
^^^^^^^ could not find futures in std
......
async fn learn_and_sing() {
learn_sone().await;
sing_song().await;
}
......
fn main() {
block_on(learn_and_sing());
^^^^^^^^ not found in this scope
}

Demo I saw in Asynchronous Programming in Rust。 Thank you。

I don't think block_on is in std (yet). You need to import that from the futures crate. So you can probably just remove the std:: from there.

Roughly speaking, the standard library has only an interface for Futures, but no implementation. You still need (and probably will always need) a 3rd party crate to implement the futures.

Rust the language only knows how to create futures, which by themselves don't do anything. You need a runtime like async-std or tokio to run the futures. That's not a bug or missing feature, but an intentional flexibility of the design, because there are different ways of executing futures with different trade-offs.

Also note that std::future has just been released very recently, so very few libraries (mostly alpha versions) have support for them.

Rust the language doesn't even really know how to create futures (other than trivial ones that complete instantly). What the async/await syntax gives is the ability to compose futures.

You need to bring in both a source of leaf-level non-trivial futures (e.g. timers, IO) and something that can run them; async-std and tokio both provide both of these things, but there are other crates like futures-intrusive that just provide leaf-futures which can run on any runtime.

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