I wanted to create a function that returns a data structure with all possible combinations for a certain group of numbers: e.g.: for [1, 2, 3] return [[1], [2], [3], [1, 2], [2,1], ..., [1, 2, 3], [2, 1, 3], ...[3, 2, 1]].
I understand that c and p are some kind of vector of &integer, but I cannot find a way to save them into an array or vector. I was trying to save it as a vector of vectors since I do not think it is possible to save them as a vector of arrays, since the arrays have different sizes. It's also not possible as an array of vectors since I do not know the number of combinations at the start.
How can I store all c and p in a data structure so that it can be returned and used outside?
use permutator::{Combination, Permutation}; // 0.3.3
pub fn init() -> Vec<Vec<u32>> {
let actions: Vec<Vec<u32>>;
let mut data = &[1, 2, 3];
let mut counter = 1;
for i in 1..=data.len() {
data.combination(i).for_each(|mut c| {
println!("{:?}", c);
actions.push(c);
c.permutation().for_each(|p| {
println!("k-perm@{} = {:?}", counter, p);
counter += 1;
actions.push(p);
});
});
}
actions
}
The error I'm getting is:
error[E0308]: mismatched types
--> src/main.rs:10:26
|
10 | actions.push(c);
| ^ expected u32, found &{integer}
|
= note: expected type `std::vec::Vec<u32>`
found type `std::vec::Vec<&{integer}>`
error[E0308]: mismatched types
--> src/main.rs:14:30
|
14 | actions.push(p);
| ^ expected u32, found &{integer}
|
= note: expected type `std::vec::Vec<u32>`
found type `std::vec::Vec<&{integer}>`
edit:
When trying actions.push(c.iter().cloned().collect()) it returns the following error:
error[E0277]: a collection of type `std::vec::Vec<u32>` cannot be built from an iterator over elements of type `&u32`
--> src/rl.rs:59:44
|
59 | actions.push(c.iter().cloned().collect());
| ^^^^^^^ a collection of type `std::vec::Vec<u32>` cannot be built from `std::iter::Iterator<Item=&u32>`
|
= help: the trait `std::iter::FromIterator<&u32>` is not implemented for `std::vec::Vec<u32>`