sách gpt4 ai đã đi

rust - 如何在未知大小的 Vec<>> 上创建一个迭代器,它等效于迭代器在内部 Vec 上的笛卡尔积?

In lại 作者:行者123 更新时间:2023-11-29 08:04:40 28 4
mua khóa gpt4 Nike

我想设计一个类似于示例 đây 的函数除了我的情况,iproduct 的参数数量在编译时是未知的。正如 đây 所解释的那样,这在 python 中很容易完成。 .

我已经尝试使用 itertools crate,特别是 iproduct!,multi_cartesian_productcartesian_product 但目前正在与类型系统,以及不确定它是否是正确的路径。

pub fn product_of_lists(lists: &Vec<>>) -> std::slice::Iter<'_, u16> {
// generate cross products between lists
assert!(lists.len() > 2);
let mut product_iter = lists[0].iter();
for (en, list) in lists.iter().enumerate() {
if en > 0{
product_iter = iproduct!(product_iter, list.iter());
}
}
product_iter
}
error[E0308]: mismatched types
--> src/testcode.rs:44:28
|
10 | product_iter = iproduct!(product_iter, list.iter());
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected struct `std::slice::Iter`, found struct `itertools::adaptors::Product`
|
= note: expected type `std::slice::Iter<'_, _>`
found type `itertools::adaptors::Product, std::slice::Iter<'_, u16>>`
= note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)

1 Câu trả lời

doc已经回答你了:

use itertools::Itertools; // 0.8.0

pub fn product_of_lists(lists: &[Vec]) -> impl Iterator> {
// generate cross products between lists
assert!(lists.len() > 2);
lists.iter().map(|x| x.iter()).multi_cartesian_product()
}

hoặc

use itertools::Itertools; // 0.8.0

pub fn product_of_lists<'a, L, I>(lists: L) -> impl Iterator>
Ở đâu
L: IntoIterator,
I: IntoIterator,
::IntoIter: Clone,
{
lists
.into_iter()
.map(IntoIterator::into_iter)
.multi_cartesian_product()
}

关于rust - 如何在未知大小的 Vec<>> 上创建一个迭代器,它等效于迭代器在内部 Vec 上的笛卡尔积?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57517633/

28 4 0
Bài viết được đề xuất: rust - Mutex 可以引用结构但不能引用原语
Bài viết được đề xuất: mysql - MySQL 中的无限子类别排序
Bài viết được đề xuất: Java Play 2 - 手动更新
Bài viết được đề xuất: python - 使用 MySQL python 模块插入或更新行是否存在
行者123
Hồ sơ cá nhân

Tôi là một lập trình viên xuất sắc, rất giỏi!

Nhận phiếu giảm giá Didi Taxi miễn phí
Mã giảm giá Didi Taxi
Giấy chứng nhận ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com