为什么使用置换器板条箱中的迭代器将向量推入向量中,为什么会得到“预期的u32,找到的&{integer}”?]

问题描述 投票:1回答:2
我想创建一个函数,该函数返回一个数据结构,其中包含某个数字组的所有可能组合:例如:对于[1, 2, 3],返回[[1], [2], [3], [1, 2], [2,1], ..., [1, 2, 3], [2, 1, 3], ...[3, 2, 1]]

我了解cp&integer的某种向量,但是我找不到找到将它们保存到数组或向量中的方法。我试图将其保存为向量的矢量,因为我不认为有可能将它们保存为数组的矢量,因为数组的大小不同。由于我不知道开始时的组合数量,因此也不可能作为向量数组。

如何将所有cp存储在数据结构中,以便可以在外部返回和使用它?

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,...
vector types rust slice
2个回答
0
投票
我找到了解决这个问题的方法,这很丑陋,但是由于我是该语言的新手,所以我能提供的是最好的。

0
投票
尝试actions.push(c.iter().cloned().collect())时返回以下错误...
© www.soinside.com 2019 - 2024. All rights reserved.