我的 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
终端将被任务重复使用,按任意键关闭它。
为什么会发生这种情况?