要在Rust中实现迭代器,我们只需要实现
据我所知,我们需要实现特征的所有方法。例如,以下代码无法编译(playground link):
错误很明显:
next
方法,就像文档中所述的那样。但是,Iterator
特征有许多其他方法。据我所知,我们需要实现特征的所有方法。例如,以下代码无法编译(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