Eu tenho o seguinte código 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(())
}
O compilador reclama que:
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
|
Por que não está reclamando da el
variável?
E como consertar esse código? Eu gostaria de continuar usando o iterador e não for
o loop. Eu posso mudar outras partes do código, por exemplo, do_something()
posso ter protótipos diferentes.
Coloquei a c
variável aqui para proibir a solução fácil com async move {...}
. A string c
não deve ser movida.
Sem enumerate()
(e sem index
) funciona. Eu esperaria que index
(que tem usize
tipo) fosse facilmente copiável.