我正在尝试编写一个函数来转换表单中的数据结构:
input = [("a", [1,2,3]), ("b", [4,5,6])]
进入
output = [(a,1), (c,2) ..... (b,6)]
我的代码目前是这样的:
let foo=vec![('a', vec![1,2,3]), ('v', vec![2,3,4])];
let baz: Vec<(char,i32)> = foo.into_iter().map(|a|a.1.into_iter().map( |b|(a.0, b))).flatten().collect();
println!("{:?}",baz);
我收到此错误:
error[E0373]: closure may outlive the current function, but it borrows `a`, which is owned by the current function
--> src/lib.rs:10:76
|
10 | let baz: Vec<(char,i32)> = foo.into_iter().map(|a|a.1.into_iter().map( |b|(a.0, b))).flatten().collect();
| ^^^ - `a` is borrowed here
| |
| may outlive borrowed value `a`
|
note: closure is returned here
--> src/lib.rs:10:55
|
10 | let baz: Vec<(char,i32)> = foo.into_iter().map(|a|a.1.into_iter().map( |b|(a.0, b))).flatten().collect();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
help: to force the closure to take ownership of `a` (and any other referenced variables), use the `move` keyword
|
10 | let baz: Vec<(char,i32)> = foo.into_iter().map(|a|a.1.into_iter().map( move |b|(a.0, b))).flatten().collect();
| ^^^^^^^^
error[E0382]: borrow of moved value: `a`
--> src/lib.rs:10:76
|
10 | let baz: Vec<(char,i32)> = foo.into_iter().map(|a|a.1.into_iter().map( |b|(a.0, b))).flatten().collect();
| --- ^^^ - borrow occurs due to use in closure
| | |
| value moved here value borrowed here after partial move
|
= note: move occurs because `a.1` has type `std::vec::Vec<i32>`, which does not implement the `Copy` trait
我认为这意味着 Rust 不知道如何复制我的 i32s 向量,因此认为它必须改为移动 vec,但不能这样做。
我该如何解决这个问题?为 vec 实现一个 Copy 方法,还是有更好的方法来做到这一点?