• async fn 将一个代码块转换为一个 Future 对象, Future 对象维护一个状态机
  • Future 对象必须运行在一个 Executor


  • futures::executor::block_on 阻塞当前线程直到 future 完成

    // `block_on` blocks the current thread until the provided future has run to
    // completion. Other executors provide more complex behavior, like scheduling
    // multiple futures onto the same thread.
    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


  • await 异步的等待 future 完成,不阻塞当前线程,可以配合
  • futures::join! 可以同时 await 多个 future
  • futures::try_join! 如果其中一个子 future 返回错误则立即返回(join! 需要等所有 future 全部返回)
  • futures::select! 任意一个 future 完成则立即返回
async fn learn_and_sing() {
		// Wait until the song has been learned before singing it.
		// We use `.await` here rather than `block_on` to prevent blocking the
		// thread, which makes it possible to `dance` at the same time.
		let song = learn_song().await;

async fn async_main() {
		let f1 = learn_and_sing();
		let f2 = dance();

		// `join!` is like `.await` but can wait for multiple futures concurrently.
		// If we're temporarily blocked in the `learn_and_sing` future, the `dance`
		// future will take over the current thread. If `dance` becomes blocked,
		// `learn_and_sing` can take back over. If both futures are blocked, then
		// `async_main` is blocked and will yield to the executor.
		futures::join!(f1, f2);

fn main() {