[1, 2, 3]
,返回[[1], [2], [3], [1, 2], [2,1], ..., [1, 2, 3], [2, 1, 3], ...[3, 2, 1]]
。我了解c
和p
是&integer
的某种向量,但是我找不到找到将它们保存到数组或向量中的方法。我试图将其保存为向量的矢量,因为我不认为有可能将它们保存为数组的矢量,因为数组的大小不同。由于我不知道开始时的组合数量,因此也不可能作为向量数组。
如何将所有c
和p
存储在数据结构中,以便可以在外部返回和使用它?
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
}
我得到的错误是:
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:
actions.push(c.iter().cloned().collect())
时,它会返回以下错误: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>`
我想创建一个函数,该函数返回具有一组特定数字的所有可能组合的数据结构:例如:对于[1、2、3],返回[[1],[2],[3],[1 ,2],[2,1],...,[1,2,3],[2,...
尝试actions.push(c.iter().cloned().collect())
时返回以下错误...