如何修复“关闭”可能会超过当前功能。

发布于 2025-02-03 09:55:43 字数 2372 浏览 4 评论 0原文

我有这个代码:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浮光之海 2025-02-10 09:55:43

要在移动闭合中通过引用捕获某些内容,请在闭合外创建一个参考,然后在闭合中使用该引用;该引用将移至闭合,而不是其引用的值。

  row_max.iter().enumerate()
    .flat_map(|(row, cols)| {
        // Shadow col_min with a reference to the outer variable.  The closure
        // will capture this reference instead of the col_min value above.
        let col_min = &col_min;
        cols.iter()
            .filter(move |c| col_min[**c].contains(&row))
            .map(move |c| (row, *c))
    })
    .collect()

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.

  row_max.iter().enumerate()
    .flat_map(|(row, cols)| {
        // Shadow col_min with a reference to the outer variable.  The closure
        // will capture this reference instead of the col_min value above.
        let col_min = &col_min;
        cols.iter()
            .filter(move |c| col_min[**c].contains(&row))
            .map(move |c| (row, *c))
    })
    .collect()

(Playground)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文