小编luc*_*ian的帖子

为什么 tokio::spawn 不执行我的代码?

我的 Rust 代码如下所示。

#[tokio::main]
pub async fn main() {
    for i in 1..10 {
        tokio::spawn(async move {
            println!("{}", i);
        });
    }
}
Run Code Online (Sandbox Code Playgroud)

运行代码时,我希望它以随机顺序打印 1 到 10。但它只是打印一些随机数:

1
3
2
终端将被任务重复使用,按任意键关闭它。

为什么会发生这种情况?

asynchronous spawn rust async-await rust-tokio

1
推荐指数
1
解决办法
641
查看次数

标签 统计

async-await ×1

asynchronous ×1

rust ×1

rust-tokio ×1

spawn ×1