1

I am attempting to return a 2D array from a function with this prototype:

fn function1(rep1: usize, rep2: usize) -> (Vec<Vec<bool>>) {

There is this 2D vector struct in the function:

let mut array2d: Vec<Vec<bool>>;

I am using following itertools loop:

for perm in items.iter().permutations(items.len()).unique() {}

Each instance of perm is Vec<&bool> rather than Vec<bool> and so in every loop upon trying to push into array2d with array2d.push(perm); I get the following error:

note: expected struct `Vec<bool>` found struct `Vec<&bool>`

Initially I have attempted at modifying the return specifier to allow me to return 2D Vec of pointers but failed somehow at doing so. I'd be grateful if someone could show me how to dereference/cast vector of addresses as a vector of values and/or how to change return specifier so that I can return Vec<Vec<&bool>> rather than Vec<Vec<bool>>.

Full code snippet:

fn function1(rep1: usize, rep2: usize) -> (Vec<Vec<bool>>) {

    let mut array2d: Vec<Vec<bool>>;

    let mut items = vec![false; rep2];

    for r in 0..(rep1 - 1){
        items[r] = true;
    }

    for perm in items.iter().permutations(items.len()).unique() {
        array2d.push(perm); 
    }   

    return array2d;
}
mega_creamery
  • 667
  • 7
  • 19
  • Thanks for updating the question. [Here's a version using `into_iter` as the first duplicate suggests, with other errors fixed](https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=19123710023924c78c10c04e69ddd856) and [here's a version with fewer allocations](https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=abccba6477f6ad67fe16b1964fb4b133) – trent Apr 02 '21 at 17:57
  • (If your real code is doing something similar, note that `permutations().unique()` is probably going to have absolutely dismal performance characteristics for large `rep2` and you can get the same effect, in this case, by taking `(0..rep2).combinations(rep1 - 1)` and creating a `Vec` with only those elements set to `true`.) – trent Apr 02 '21 at 18:19

0 Answers0