我有以下 Rust 代码
use std::error::Error;
#[derive(Debug)]
enum MyEnum {
First,
Second,
}
fn do_something(index: usize, m: &MyEnum) {
eprintln!("do_something: {} {:?}", index, m);
}
async fn test() -> Result<(), Box<dyn Error>> {
let myvec = vec![MyEnum::First, MyEnum::Second];
let c = String::from("cap");
let futures = myvec.iter().enumerate().map(|(index, el)| async {
eprintln!("Element: {}", &c);
do_something(index, el);
});
futures::future::join_all(futures).await;
Ok(())
}
#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
test().await?;
Ok(())
}
编译器抱怨:
error[E0373]: async block may outlive the current function, but it borrows `index`, which is owned by the current function
--> src/main.rs:17:62
|
17 | let futures = myvec.iter().enumerate().map(|(index, el)| async {
| ^^^^^ may outlive borrowed value `index`
18 | eprintln!("Element: {}", &c);
19 | do_something(index, el);
| ----- `index` is borrowed here
|
为什么它没有抱怨变量el
?
如何修复此代码?我想继续使用迭代器而不是for
循环。我可以更改代码的其他部分,例如do_something()
可以有不同的原型。
我把c
变量放在这里是为了禁止使用 的简单解决方案async move {...}
。字符串c
不应该被移动。
没有enumerate()
(和没有index
) 也可以工作。我期望index
(具有usize
类型) 可以轻松复制。