How to create a matrix with iterators

Hello
I'm trying to make a better code for the snippet i created here :

let mut result: Vec<(usize, usize)> = vec![];
    for row in 0..input.len() {
        for col in 0..input[row].len() {
            if less_equals_column(input, (row, col)) && greater_equals_row(input, (row, col)) {
                result.push((row, col));
            }
        }
    }
    result

I want to do the same with iterators map and filter.
I tried this , but I don't manage to do it.

    (0..input.len()).map(|row|{
        (0..input[row].len()).into_iter().any(|col|
            less_equals_column(input, (row, col)) && greater_equals_row(input, (row, col)
    ))}).collect()

someone could help me please ?

I think you may want filter instead of any. Any returns a boolean. filter removes or retains results based on a boolean condition.

I tried that too :

    (0..input.len()).map(|row|{
        (0..input[row].len()).into_iter().filter(|&col|
            less_equals_column(input, (row, col)) && greater_equals_row(input, (row, col)
    ))}).collect()

but I have this error :

error[E0277]: a value of type `std::vec::Vec<(usize, usize)>` cannot be built from an iterator over elements of type `std::iter::Filter<std::ops::Range<usize>, [closure@src/lib.rs:4:50: 6:6 input:_, row:_]>`
 --> src/lib.rs:6:10
  |
6 |     ))}).collect()
  |          ^^^^^^^ value of type `std::vec::Vec<(usize, usize)>` cannot be built from `std::iter::Iterator<Item=std::iter::Filter<std::ops::Range<usize>, [closure@src/lib.rs:4:50: 6:6 input:_, row:_]>>`
  |
  = help: the trait `std::iter::FromIterator<std::iter::Filter<std::ops::Range<usize>, [closure@src/lib.rs:4:50: 6:6 input:_, row:_]>>` is not implemented for `std::vec::Vec<(usize, usize)>`

What is the type of input?

nested fors become .flat_maps in functional style, so you may need to replace your .map with that :wink:

I’m going to assume your types here for now.. here’s some code that compiles: (playground)

pub fn test1<T>(input: &[impl AsRef<[T]>]) -> Vec<(usize, usize)> {
    let mut result = vec![];
    for row in 0..input.len() {
        for col in 0..input[row].as_ref().len() {
            if less_equals_column(input, (row, col)) && greater_equals_row(input, (row, col)) {
                result.push((row, col));
            }
        }
    }
    result
}
pub fn test2<T>(input: &[impl AsRef<[T]>]) -> Vec<(usize, usize)> {
    (0..input.len())
        .flat_map(|row| {
            (0..input[row].as_ref().len())
                .filter(move |&col| {
                    less_equals_column(input, (row, col)) && greater_equals_row(input, (row, col))
                })
                .map(move |col| (row, col))
        })
        .collect()
}
use std::iter;
pub fn test3<T>(input: &[impl AsRef<[T]>]) -> Vec<(usize, usize)> {
    input
        .iter()
        .enumerate()
        .flat_map(|(row, input_row)| iter::repeat(row).zip(0..input_row.as_ref().len()))
        .filter(|&(row, col)| {
            less_equals_column(input, (row, col)) && greater_equals_row(input, (row, col))
        })
        .collect()
}

Thank you , and thanks @Yandros .
It works like that !