代码块内部有一个 type Item,这是在声明迭代器的每个实现都有一个名为 Item 的关联类型。在这种情况下,当您调用 next 方法时,它可以返回这种类型。
Iterator 返回 Option<Self::Item> 的原因是该接口合并了 has_next 和 get_next 概念。 当拥有下一个值时,将返回 Some(value),当没有下一个值时,将返回 None。 这使得 API 在使用和实现上更符合人体工程学,更安全,同时避免了冗余的检查和 has next 和 get next 之间的逻辑。 太棒了!
实际上,我们已经有了使用List接口实现 IntoIter 的所有工具:只需一遍又一遍地调用 pop。因此,我们只需要将 IntoIter 作为 List 的新型包装器实现:
// Tuple structs are an alternative form of struct,// useful for trivial wrappers around other types.pubstructIntoIter<T>(List<T>);impl<T> List<T> {pubfninto_iter(self) ->IntoIter<T> {IntoIter(self) }}impl<T> IteratorforIntoIter<T> {typeItem=T;fnnext(&mut self) ->Option<Self::Item> {// access fields of a tuple struct numerically self.0.pop() }}
让我们写一个测试:
#[test]fninto_iter() {letmut list =List::new(); list.push(1); list.push(2); list.push(3);letmut iter = list.into_iter();assert_eq!(iter.next(), Some(3));assert_eq!(iter.next(), Some(2));assert_eq!(iter.next(), Some(1));assert_eq!(iter.next(), None);}