【发布时间】:2019-03-14 23:28:45
【问题描述】:
我正在尝试使用 Rust itertools 生成日期范围的所有组合,但它说不满足特征界限。
extern crate chrono;
extern crate itertools;
use itertools::Itertools;
use chrono::prelude::*;
fn main() {
let min = NaiveDate::from_ymd(2018, 10, 1);
let max = NaiveDate::from_ymd(2018, 10, 14);
let combinations = (min..=max).combinations(5);
}
错误信息:
error[E0599]: no method named `combinations` found for type `std::ops::RangeInclusive<chrono::NaiveDate>` in the current scope
--> src/main.rs:46:36
|
46 | let combinations = (min..=max).combinations(5);
| ^^^^^^^^^^^^
|
= note: the method `combinations` exists but the following trait bounds were not satisfied:
`std::ops::RangeInclusive<chrono::NaiveDate> : itertools::Itertools`
`&std::ops::RangeInclusive<chrono::NaiveDate> : itertools::Itertools`
`&mut std::ops::RangeInclusive<chrono::NaiveDate> : itertools::Itertools`
我希望 Itertools 是为通用 RangeInclusive 实现的。我正在学习 Rust,所以我可能会遗漏一些明显的东西。
【问题讨论】:
-
问题的根本原因是
chrono没有实现Iterator的日期类型范围。 -
我明白了,
itertools不是为范围实现的,它们是为Iterator实现的,这对于一个范围来说并不总是可能的(例如,Range<f32>)但它可能是可能的对于Range<NaiveDate>但chrono没有实现它。 -
这样组合的结果是什么?
-
它是一个数学组合的迭代器,是迭代器给定大小元素的子集。我不知道你为什么需要它来约会。另一方面,简单的日期范围对日历非常有用。
标签: rust