tokio::spawn(async { … name … }) 报「may outlive」是最常见的 async 编译错误。这篇从错误信息出发,验证三种修法,再说明 'static 到底限制什么、什么时候用 join! 就不用考虑它。
error[E0373]: async block may outlive the current function, but it borrows `name`, which is owned by the current function
--> src/main.rs:4:26
|
4 | let h = tokio::spawn(async { println!("hello {}", name); });
| ^^^^^ ---- `name` is borrowed here
| |
| may outlive borrowed value `name`
|
= note: async blocks are not executed immediately and must either take a reference or ownership of outside variables they use
help: to force the async block to take ownership of `name` (and any other referenced variables), use the `move` keyword
|
4 | let h = tokio::spawn(async move { println!("hello {}", name); });
| ++++
For more information about this error, try `rustc --explain E0373`.
error: could not compile `u100a` (bin "u100a") due to 1 previous error线程的类似限制见线程创建与 Join;调度见Tokio 任务调度。
编译退出码 0 ① move 进任务,任务拥有它: tokio ② 3 个任务共享 Arc<Vec>:[10, 20, 30](strong_count = 1) ③ clone 之后原来的 name 仍然可用: tokio
编译退出码 0 join! 里的两个 async 块借用了 name 和 data:tokio 长度 5 / 6;name 与 data 之后仍然可用: tokio [1, 2, 3]
error[E0597]: `local` does not live long enough
--> src/main.rs:7:28
|
7 | let local = 5; let r = &local;
| ----- ^^^^^^ borrowed value does not live long enough
| |
| binding `local` declared here
8 | needs_static(r);
| --------------- argument requires that `local` is borrowed for `'static`
9 | }
| - `local` dropped here while still borrowed
|
note: requirement that the value outlives `'static` introduced here
--> src/main.rs:1:20编译退出码 0 任务内部借用自己的 v,跨 await 也没问题:10