如何修复“关闭”可能会超过当前功能。
我有这个代码:
pub fn find_saddle_points(input: &[Vec<u64>]) -> Vec<(usize, usize)> {
let mut row_max: Vec<HashSet<usize>> = vec![HashSet::new(); input.len()];
let mut col_min: Vec<HashSet<usize>> = vec![HashSet::new(); input[0].len()];
...
row_max.iter().enumerate()
.flat_map(|(row, cols)| {
cols.iter()
.filter(|c| col_min[**c].contains(&row))
.map(|c| (row, *c))
})
.collect()
}
编译失败。
error[E0373]: closure may outlive the current function, but it borrows `row`, which is owned by the current function
--> src/lib.rs:48:21
|
48 | .filter(|c| col_min[**c].contains(&row))
| ^^^ --- `row` is borrowed here
| |
| may outlive borrowed value `row`
|
note: closure is returned here
--> src/lib.rs:47:9
|
47 | / cols.iter()
48 | | .filter(|c| col_min[**c].contains(&row))
49 | | .map(|c| (row, *c))
| |_______________________________^
help: to force the closure to take ownership of `row` (and any other referenced variables), use the `move` keyword
|
48 | .filter(move |c| col_min[**c].contains(&row))
| ++++
error[E0373]: closure may outlive the current function, but it borrows `row`, which is owned by the current function
--> src/lib.rs:49:18
|
49 | .map(|c| (row, *c))
| ^^^ --- `row` is borrowed here
| |
| may outlive borrowed value `row`
|
note: closure is returned here
--> src/lib.rs:47:9
|
47 | / cols.iter()
48 | | .filter(|c| col_min[**c].contains(&row))
49 | | .map(|c| (row, *c))
| |_______________________________^
help: to force the closure to take ownership of `row` (and any other referenced variables), use the `move` keyword
|
49 | .map(move |c| (row, *c))
| ++++
我可以在MAP
中使用MOOV
,因为没有人在此之后使用row
,但是我不能使用移动
在filter
中,由于col_min
具有类型vec&lt; ashshet&lt; usize&gt;&gt;
,该不实现copy> copy> copy
特征。如何解决这个问题?
I've this code:
pub fn find_saddle_points(input: &[Vec<u64>]) -> Vec<(usize, usize)> {
let mut row_max: Vec<HashSet<usize>> = vec![HashSet::new(); input.len()];
let mut col_min: Vec<HashSet<usize>> = vec![HashSet::new(); input[0].len()];
...
row_max.iter().enumerate()
.flat_map(|(row, cols)| {
cols.iter()
.filter(|c| col_min[**c].contains(&row))
.map(|c| (row, *c))
})
.collect()
}
Compilation fails.
error[E0373]: closure may outlive the current function, but it borrows `row`, which is owned by the current function
--> src/lib.rs:48:21
|
48 | .filter(|c| col_min[**c].contains(&row))
| ^^^ --- `row` is borrowed here
| |
| may outlive borrowed value `row`
|
note: closure is returned here
--> src/lib.rs:47:9
|
47 | / cols.iter()
48 | | .filter(|c| col_min[**c].contains(&row))
49 | | .map(|c| (row, *c))
| |_______________________________^
help: to force the closure to take ownership of `row` (and any other referenced variables), use the `move` keyword
|
48 | .filter(move |c| col_min[**c].contains(&row))
| ++++
error[E0373]: closure may outlive the current function, but it borrows `row`, which is owned by the current function
--> src/lib.rs:49:18
|
49 | .map(|c| (row, *c))
| ^^^ --- `row` is borrowed here
| |
| may outlive borrowed value `row`
|
note: closure is returned here
--> src/lib.rs:47:9
|
47 | / cols.iter()
48 | | .filter(|c| col_min[**c].contains(&row))
49 | | .map(|c| (row, *c))
| |_______________________________^
help: to force the closure to take ownership of `row` (and any other referenced variables), use the `move` keyword
|
49 | .map(move |c| (row, *c))
| ++++
I can use move
in the map
since no one uses row
after it, but I can't use move
in the filter
since col_min
has type Vec<HashSet<usize>>
, which does not implement the Copy
trait. How to fix this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
要在
移动
闭合中通过引用捕获某些内容,请在闭合外创建一个参考,然后在闭合中使用该引用;该引用将移至闭合,而不是其引用的值。(
To capture something by reference in a
move
closure, create a reference outside of the closure and then use that reference in the closure; the reference will be moved into the closure instead of the value it references.(Playground)