【发布时间】:2016-10-11 01:19:57
【问题描述】:
我正在尝试扩展 Iterator 特征的功能。
我的statistics/iter_statistics.rs:
mod iter_statistics {
pub trait IterStatistics: Iterator<Item = f64> {
fn foo(&mut self) -> f64 {
0.0
}
}
impl IterStatistics for Iterator<Item = f64> {}
}
还有statistics/mod.rs:
pub use self::iter_statistics::*;
mod iter_statistics;
最后在我的测试代码中
use statistics::IterStatistics;
fn main() {
let z: Vec<f64> = vec![0.0, 3.0, -2.0];
assert_eq!(z.into_iter().foo(), 0.0);
}
当我运行测试时,我得到:
error: no method name `foo` found for type `std::vec::IntoIter<f64>` in the current scope
assert_eq!(z.into_iter().foo(), 0.0);
^~~
这对我来说很奇怪,因为 docs 的 IntoIter<T> 说它实现了 Iterator<Item=T>。
【问题讨论】:
-
这是一个侧面,但如上所述,您有一个模块
iter_statistics::iter_statistics,这可能不是您的意思;在以它命名的文件中不需要mod {}。 -
@ChrisEmerson 是的,这是我在编写示例代码时的冗余:)
标签: rust