從std::iter::Iterator
文件,我可以看到,只需要next
方法:Rust如何知道需要或提供哪些特徵方法?
所需的方法
fn next(&mut self) -> Option<Self::Item>
但是從source code,後刪除註釋:
pub trait Iterator {
/// The type of the elements being iterated over.
#[stable(feature = "rust1", since = "1.0.0")]
type Item;
......
#[stable(feature = "rust1", since = "1.0.0")]
fn next(&mut self) -> Option<Self::Item>;
......
#[inline]
#[stable(feature = "rust1", since = "1.0.0")]
fn size_hint(&self) -> (usize, Option<usize>) { (0, None) }
......
}
我可以看到,除了#[inline]
屬性外,所需方法和提供方法之間沒有區別。 Rust如何知道需要或提供哪種方法?