gpt4 book ai didi

Rust 错误 [E0373] : closure may outlive the current function, 但它借用了 `iteration_index`

转载 作者:行者123 更新时间:2023-11-29 08:35:03 26 4
gpt4 key购买 nike

我有两个向量 itertools::MinMaxResult .我需要遍历第一个向量,对于每个元素,遍历第二个向量,检查第一个向量的最小值是否等于第二个向量的任何元素的最大值,反之亦然。这是我尝试过的 MCVE:

use itertools::MinMaxResult; // itertools = "0.8.0"
use itertools::MinMaxResult::*;

pub fn mcve() -> Vec<(usize, usize)> {

// dummy variables to make the MCVE compile
let num_rows = 0;
let num_cols = 0;
let row_minmax: Vec<MinMaxResult<&u64>> = vec![];
let col_minmax: Vec<MinMaxResult<&u64>> = vec![];

// Problematic code:
(0..num_rows)
.flat_map(|row_index| {
(0_usize..num_cols).filter_map(|col_index| {
match (row_minmax[row_index], col_minmax[col_index]) {
(MinMax(a, _b), MinMax(_c, d)) if a == d =>
Some((row_index, col_index)),
(MinMax(_a, b), MinMax(c, _d)) if b == c =>
Some((row_index, col_index)),
_ => None,
}
})
})
.collect::<Vec<(usize, usize)>>()
}

Link to the Playground with full code

我收到以下错误:

error[E0373]: closure may outlive the current function, but it borrows `row_index`,
which is owned by the current function
--> src/main.rs:15:48
|
15 | (0_usize..num_cols).filter_map(|col_index| {
| ^^^^^^^^^^^ may outlive
borrowed value `row_index`
16 | match (row_minmax[row_index], col_minmax[col_index]) {
| --------- `row_index` is borrowed here
|
note: closure is returned here
--> src/main.rs:15:17
|
15 | / (0_usize..num_cols).filter_map(|col_index| {
16 | | match (row_minmax[row_index], col_minmax[col_index]) {
17 | | (MinMax(a, _b), MinMax(_c, d)) if a == d =>
Some((row_index, col_index)),
18 | | (MinMax(_a, b), MinMax(c, _d)) if b == c =>
Some((row_index, col_index)),
19 | | _ => None,
20 | | }
21 | | })
| |__________________^
help: to force the closure to take ownership of `row_index`
(and any other referenced variables), use the `move` keyword
|
15 | (0_usize..num_cols).filter_map(move |col_index| {
| ^^^^^^^^^^^^^^^^

如果我按照编译器的建议添加一个move,我会得到两倍的错误,所以这无济于事。我该如何摆脱这个错误?

最佳答案

您的链接要点有一大堆典型的“与借用检查器搏斗”的错误。以下是解决它们的方法:

  • 按照编译器的建议使用move,但要确保所有移动的值都实现了Copy
  • usize 类型的迭代索引已经实现了Copy,因此这里无需执行任何操作。
  • 向量有问题,所以只需获取它们的不可变引用,然后将不可变引用传递到闭包中。
  • 您的into_par_iter 导致不相关的所有权问题。只需使用 par_iter 代替,然后您将迭代引用,而不是实际值,并且在迭代时不会破坏矩阵。

这里编译:

use itertools::MinMaxResult; // itertools = "0.8.0"
use itertools::MinMaxResult::*;
use itertools::Itertools;
use rayon::prelude::*; // rayon = "1.0.3"

pub fn find_saddle_points(input: &[Vec<u64>]) -> Vec<(usize, usize)> {
let flattened_matrix: Vec<(&u64)> = input.into_par_iter().flatten().collect();
if flattened_matrix.is_empty() {
vec![]
} else {
let num_rows = input.len();
let num_cols = input[0].len();

let row_minmax: Vec<MinMaxResult<&u64>> = input
.iter()
.map(|row| row.iter().minmax())
.collect::<Vec<MinMaxResult<&u64>>>();

let input_tranpose: Vec<Vec<u64>> = (0_usize..num_cols)
.into_par_iter()
.map(|col_index| {
(0_usize..num_rows)
.map(|row_index| input[row_index][col_index])
.collect::<Vec<u64>>()
})
.collect();

// don't take ownership!
// Use `par_iter` instead, then `col` will be a reference,
// and the borrow-checker is happy.
let col_minmax: Vec<MinMaxResult<&u64>> = input_tranpose
.par_iter()
.map(|col| col.iter().minmax())
.collect();

let rmm_ref = &row_minmax;
let cmm_ref = &col_minmax;
(0_usize..num_rows)
.flat_map(|row_index| {
(0_usize..num_cols).filter_map(move |col_index| {
match (rmm_ref[row_index], cmm_ref[col_index]) {
(MinMax(a, _b), MinMax(_c, d)) if a == d =>
Some((row_index, col_index)),
(MinMax(_a, b), MinMax(c, _d)) if b == c =>
Some((row_index, col_index)),
_ => None,
}
})
})
.collect::<Vec<(usize, usize)>>()
}
}

fn main(){}

关于Rust 错误 [E0373] : closure may outlive the current function, 但它借用了 `iteration_index`,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54596442/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com