【发布时间】:2021-03-18 02:24:24
【问题描述】:
我想在itertools中创建一个类似于collect_vec()函数的方法。
复制itertools代码创建一个小例子:
pub trait MyItertools: Iterator {
fn collect_vec(self) -> Vec<Self::Item>
where Self: Sized
{
self.collect()
}
}
fn main() {
let v = (0..5).collect_vec();
println!("{:?}", v);
}
我相当天真地期望编译器会使用我的collect_vec 作为
MyItertools 在范围内。
itertools 必须使用其他魔法来编译它。
我们得到错误:
error[E0599]: no method named `collect_vec` found for struct `std::ops::Range<{integer}>` in the current scope
--> src/main.rs:14:20
|
14 | let v = (0..5).collect_vec();
| ^^^^^^^^^^^ method not found in `std::ops::Range<{integer}>`
|
= help: items from traits can only be used if the trait is implemented and in scope
note: `MyItertools` defines an item `collect_vec`, perhaps you need to implement it
--> src/main.rs:5:1
|
5 | pub trait MyItertools: Iterator {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: aborting due to previous error
这具有误导性,因为我们确实在 MyItertools 中实现了 collect_vec。
一个可能的解决方案是impl MyItertools
Iterator,但似乎没有办法为所有实现特征和类型的Iterator 执行此操作。
【问题讨论】:
-
不,不是,因为您实际上还没有为
std::ops::Range实现MyItertools特征(这就是(0..5)的含义)。你已经声明了这个特征,但你实际上并没有为任何类型实现它(至少在你的 MCVE 中)。要在SomeType上实现特征,您必须添加一个impl MyItertools for SomeType块(并确保SomeType也实现Iterator)。 -
谢谢,EvilTalk。正如我在编辑中建议的那样,我使用的所有类型都可以这样做,但 Itertools 似乎没有这样做,因此很神奇。
标签: generics rust iterator traits scoping