I'm looking for a function with the signature fn product(vector: &Vec<i32>, n: &i32) -> Vec<Vec<i32>>
or similar. An example:
assert_eq!(
product(vec![1, 2, 3, 4], 2),
[
[1, 1],
[1, 2],
[1, 3],
[1, 4],
[2, 1],
[2, 2],
[2, 3],
[2, 4],
[3, 1],
[3, 2],
[3, 3],
[3, 4],
[4, 1],
[4, 2],
[4, 3],
[4, 4]
],
)
I have tried using iproduct
from the itertools
crate:
use itertools::iproduct; // 0.10.0
fn product(vector: &Vec<i32>, n: &i32) -> Vec<Vec<i32>> {
let mut result = Vec::new();
for _ in 0..*n {
result = iproduct!(result.iter(), vector.iter()).collect();
}
result
}
Which produces this error:
error[E0277]: a value of type `Vec<_>` cannot be built from an iterator over elements of type `(&_, &i32)`
--> src/lib.rs:7:58
|
7 | result = iproduct!(result.iter(), vector.iter()).collect();
| ^^^^^^^ value of type `Vec<_>` cannot be built from `std::iter::Iterator<Item=(&_, &i32)>`
|
= help: the trait `FromIterator<(&_, &i32)>` is not implemented for `Vec<_>`
How do I solve this problem?