rust - 尝试收集到向量中但失败并返回 "a collection cannot be built over elements of type <...>"

标签 rust rayon

我试图找到一个矩阵的鞍点,这是一个借用的向量数组。出于此目的,鞍点是矩阵中的一个元素,它要么在其列中最小,在其行中最大,要么在其列中最大,在其行中最小。

use rayon::prelude::*;

pub fn find_saddle_points(input: &[Vec<u64>]) -> Vec<(usize, usize)> {
    let flattened_matrix: Vec<(&u64)> = input.into_par_iter().flatten().collect::<Vec<&u64>>();
    match flattened_matrix == vec![] { //Check for empty input
        true => vec![],
        false => {
            let num_rows: usize = input.len();
            let num_cols: usize = input[0].len();

            let row_minima: Vec<Option<&u64>> = (0_usize..(num_rows - 1_usize))
                .into_par_iter()
                .map(|row_index| input[row_index].iter().min())
                .collect::<Vec<Option<&u64>>>();

            let row_maxima: Vec<Option<&u64>> = (0_usize..(num_rows - 1_usize))
                .into_par_iter()
                .map(|row_index| input[row_index].iter().max())
                .collect::<Vec<Option<&u64>>>();

            let input_tranpose: Vec<Vec<u64>> = (0_usize..(num_cols - 1_usize)) //Transpose the matrix to make it easier to get column information
                .into_par_iter()
                .map(|col_index| {
                    (0_usize..(num_rows - 1_usize))
                        .map(|row_index| input[row_index][col_index])
                        .collect::<Vec<u64>>() //Individual column
                })
                .collect::<Vec<Vec<u64>>>();

            let col_minima: Vec<Option<&u64>> = (0_usize..(num_cols - 1_usize))
                .into_par_iter()
                .map(|col_index| input_tranpose[col_index].iter().min())
                .collect::<Vec<Option<&u64>>>();

            let col_maxima: Vec<Option<&u64>> = (0_usize..(num_cols - 1_usize))
                .into_par_iter()
                .map(|col_index| input_tranpose[col_index].iter().max())
                .collect::<Vec<Option<&u64>>>();

            //All fine up to this point

            (0_usize..(num_rows - 1_usize))
                .map(|row_index| {
                    (0_usize..(num_cols - 1_usize)).map(|col_index| {
                        match (Some(row_minima[row_index]) == Some(col_maxima[col_index])
                            || Some(row_maxima[row_index]) == Some(col_minima[col_index]))
                        {
                            true => Some((row_index, col_index)),
                            false => None,
                        }
                    })
                })
                .collect::<Vec<(usize, usize)>>()
        }
    }
}

错误在最后的.collect:

error[E0277]: a collection of type `std::vec::Vec<(usize, usize)>` cannot be built from an iterator over elements of type `std::iter::Map<std::ops::Range<usize>, [closure@src/lib.rs:45:57: 52:22 row_minima:_, row_index:_, col_maxima:_, row_maxima:_, col_minima:_]>`
  --> src/lib.rs:54:18
   |
54 |                 .collect::<Vec<(usize, usize)>>()
   |                  ^^^^^^^ a collection of type `std::vec::Vec<(usize, usize)>` cannot be built from `std::iter::Iterator<Item=std::iter::Map<std::ops::Range<usize>, [closure@src/lib.rs:45:57: 52:22 row_minima:_, row_index:_, col_maxima:_, row_maxima:_, col_minima:_]>>`
   |
   = help: the trait `std::iter::FromIterator<std::iter::Map<std::ops::Range<usize>, [closure@src/lib.rs:45:57: 52:22 row_minima:_, row_index:_, col_maxima:_, row_maxima:_, col_minima:_]>>` is not implemented for `std::vec::Vec<(usize, usize)>`

我知道这是由于迭代器受到 collect 函数的影响不是预期的类型,但对我来说它应该是正确的,但我不清楚为什么会这样't.

最佳答案

(The chat transcript 将指导您解决这个问题,因为它有点复杂)

TL;DR 使用 Iterator::flat_map 的组合和 Iterator::filter_map解决问题。

(0_usize..(num_rows - 1_usize))
    .flat_map(|row_index| {
        (0_usize..(num_cols - 1_usize)).flat_map(|col_index| {
            match row_minima[row_index] == col_maxima[col_index]
                || row_maxima[row_index] == col_minima[col_index]
            {
                true => Some((row_index, col_index)),
                false => None,
            }
        })
    })
    .collect::<Vec<(usize, usize)>>()

请注意,此代码非常不合常理,但显示了解决方案。

关于rust - 尝试收集到向量中但失败并返回 "a collection cannot be built over elements of type <...>",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54592637/

相关文章:

rust - Rayon 无法将 .chars() 迭代器转换为 .par_iter()

Rust 线​​程池每个线程中都有初始化代码?

compiler-errors - 具有泛型编译错误的 Rust 结构

rust - 有没有办法在文档注释中内联常量(由 cargo 文档呈现)?

rust - 创建特征对象的副本

rust - 为什么向量的 Rust 索引语法不取得元素的所有权?

rust - 在 Rust 的范围内找不到来自包含的 Trait 实现的方法

rust - 如何获取Rayon的par_chunks_mut中的chunk index

rust - 如何找到合适的组合器以使此代码与rayon一起使用?

multithreading - Rayon 会避免为少量工作生成线程吗?