如何解决当将tokio :: spawn与具有可变引用的未来一起使用时,“借入的价值不能长期使用”
以下代码无法编译,因为编译器无法保证hashmap_of_lists
的寿命足够长。我无法克服这个问题。
我尝试使用Arc
和Mutex
,但是由于some_func
的异步方式并在内部使用Mutex
,我遇到了其他问题。
use futures; // 0.3.5
use std::collections::HashMap;
use tokio; // 0.2.21
async fn some_func(_some_slice: &mut [String]) {}
#[tokio::main]
async fn main() {
let mut hashmap_of_lists = HashMap::<String,Vec<String>>::new();
let mut join_handles = Vec::new();
for (_,value) in hashmap_of_lists.iter_mut() {
let future = some_func(value);
let join_handle = tokio::task::spawn(future);
join_handles.push(join_handle);
}
futures::future::join_all(join_handles).await;
}
我收到此错误
error[E0597]: `hashmap_of_lists` does not live long enough
--> src/main.rs:12:23
|
12 | for (_,value) in hashmap_of_lists.iter_mut() {
| ^^^^^^^^^^^^^^^^-----------
| |
| borrowed value does not live long enough
| argument requires that `hashmap_of_lists` is borrowed for `'static`
...
19 | }
| - `hashmap_of_lists` dropped here while still borrowed
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。