【发布时间】:2017-03-23 20:05:37
【问题描述】:
要在 Rust 中实现迭代器,我们只需要实现 next 方法,正如 in the documentation 解释的那样。但是,Iterator 特征 has many more methods。
据我所知,我们需要实现一个 trait 的所有方法。例如,这不会编译 (playground link):
struct SomeStruct {}
trait SomeTrait {
fn foo(&self);
fn bar(&self);
}
impl SomeTrait for SomeStruct {
fn foo(&self) {
unimplemented!()
}
}
fn main() {}
错误很明显:
error[E0046]: not all trait items implemented, missing: `bar`
--> src/main.rs:8:1
|
5 | fn bar(&self);
| -------------- `bar` from trait
...
8 | impl SomeTrait for SomeStruct {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ missing `bar` in implementation
【问题讨论】: