view src/async_test/src/main.rs @ 12:70ab6c2f7f6e

fu_test worked.
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Mon, 18 Jan 2021 20:52:32 +0900
parents 7eb649571bc6
children afac42f2b948
line wrap: on
line source

use futures::executor::block_on;

struct Song {

}

async fn learn_song() -> Song {
    println!("learn");
    Song {} }
async fn sing_song(song: Song) {
    println!("sing_song")
}
async fn dance() {
    println!("dance");
}

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;
    sing_song(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() {
    let m = async_main();
    println!("waiting");
    block_on(m);
    async_test::fu_test::fu_test();
}