zoukankan      html  css  js  c++  java
  • Asynchronous Programming in Rust

    根据   Asynchronous Programming in Rust  (https://rust-lang.github.io/async-book/01_getting_started/04_async_await_primer.html)

    整理的代码:

    use futures::executor::block_on;
    struct  Song();
    async fn learn_song() -> Song { println!("learn_song");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() {
        block_on(async_main());
    }
  • 相关阅读:
    UVALive 3664:Guess(贪心 Grade E)
    uva 1611:Crane(构造 Grade D)
    uva 177:Paper Folding(模拟 Grade D)
    UVALive 6514:Crusher’s Code(概率dp)
    uva 11491:Erasing and Winning(贪心)
    uva 1149:Bin Packing(贪心)
    uva 1442:Cave(贪心)
    学习 linux第一天
    字符编码问题
    orm 正向查询 反向查询
  • 原文地址:https://www.cnblogs.com/pu369/p/15262542.html
Copyright © 2011-2022 走看看