1

我正在尝试编写一个函数来转换表单中的数据结构:

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 方法,还是有更好的方法来做到这一点?

4

2 回答 2

4

当你调用a.1.into_iter(),a被移动,并且不能再在内部闭包中借用。

最简单的解决方案是解构a,因此每个组件都可以单独借用/移动:

.map(|(c, v)| v.into_iter().map(move |b| (c, b)))

还要注意move关键字,这意味着它c被移动到内部闭包中,因此允许它比外部闭包寿命更长。

于 2020-04-03T00:13:26.377 回答
0

IntoIterator消费并产生价值。由于Vec未实现Copy,因此当您调用时a.1.into_iter(),它会被移动。你可以像这样克隆它:a.1.clone().into_iter()

此外,您想使用关键字来获取闭包中move的所有权。a

let baz: Vec<(char, i32)> = foo
    .into_iter()
    .map(|a| a.1.clone().into_iter().map(move |b| (a.0, b)))
    .flatten()
    .collect();
println!("{:?}", baz);
// [('a', 1), ('a', 2), ('a', 3), ('v', 2), ('v', 3), ('v', 4)]
于 2020-04-02T23:52:42.423 回答