V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX  ›  0ioaths  ›  全部回复第 1 页 / 共 1 页
回复总数  2
38 天前
回复了 youngPacce 创建的主题 Rust rust 关于.await 的疑惑
将 `hello_cat` 和 `hello_dog` 作为异步任务执行就是期望的效果,以 `tokio runtime` 为例

```rust
use tokio::{runtime, time};

async fn hello_world() {
tokio::spawn(hello_cat());
tokio::spawn(hello_dog());

let five_secs = time::Duration::from_secs(5);
time::sleep(five_secs).await;
println!("hello, world!");
}

async fn hello_cat() {
println!("hello, kitty!");

let three_secs = time::Duration::from_secs(3);
time::sleep(three_secs).await;
println!("hello, kitty!2");
}

async fn hello_dog() {
println!("hello, snoopy!");
}

fn main(){
runtime::Builder::new_multi_thread()
.worker_threads(4)
.enable_all()
.build()
.unwrap()
.block_on(hello_world());
}

```

```shell
hello, kitty!
hello, snoopy!
hello, kitty!2 // after 3 sces
hello, world! // after 5 secs
```
关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   996 人在线   最高记录 6679   ·     Select Language
创意工作者们的社区
World is powered by solitude
VERSION: 3.9.8.5 · 14ms · UTC 20:55 · PVG 04:55 · LAX 13:55 · JFK 16:55
Developed with CodeLauncher
♥ Do have faith in what you're doing.