[英]What is the Item in a nested struct that implements the Iterator trait?
I am watching a video on Rust iterators and the video host implements a slightly more complex struct Flatten
.我正在观看关于 Rust 迭代器的视频,视频主机实现了一个稍微复杂的 struct
Flatten
。 In essence, this Flatten
struct holds items, each of which implements the IntoIterator
trait so that each item inside Flatten
( Note that the following code is not the final code from the video and is only used to show my confusion ):本质上,这个
Flatten
结构包含项目,每个项目都实现了IntoIterator
特性,以便Flatten
中的每个项目(请注意,以下代码不是视频中的最终代码,仅用于显示我的困惑):
pub struct Flatten<O>
{
outer: O,
}
impl<O> Flatten<O> {
fn new(iter: O) -> Self {
Flatten { outer: iter }
}
}
Then there is the part where Flatten
implements the Iterator
trait:然后是
Flatten
实现Iterator
trait 的部分:
impl<O> Iterator for Flatten<O>
where
O: Iterator,
O::Item: IntoIterator,
{
type Item = <O::Item as IntoIterator>::Item;
fn next(&mut self) -> Option<Self::Item> {
self.outer.next().and_then(|inner| inner.into_iter().next())
}
}
What I am very confused about is that when we set the constraint O::Item: IntoIterator
, we obviously meant that the generic type O
has items that implement IntoIterator
.我很困惑的是,当我们设置约束
O::Item: IntoIterator
,我们显然意味着泛型类型O
具有实现IntoIterator
项目。
Edit 1: To clarify, my confusion was that we first said that O::Item: IntoIterator
and then specified again type Item = <O::Item as IntoIterator>::Item
.编辑1:澄清一下,我的困惑是我们首先说
O::Item: IntoIterator
然后再次指定type Item = <O::Item as IntoIterator>::Item
。 Are the Item
s appearing in these two clauses referring to different concepts?出现在这两个子句中的
Item
是指不同的概念吗?
Edit 2: I think I understand it now.编辑2:我想我现在明白了。 An
Iterator
has Item
, so we could write O::Item: IntoIterator
. Iterator
有Item
,所以我们可以写O::Item: IntoIterator
。 And when we define type Item = <O::Item as IntoIterator>::Item
, we are specifying the return Item type for Flatten
, not for O
.当我们将
type Item = <O::Item as IntoIterator>::Item
定义type Item = <O::Item as IntoIterator>::Item
,我们指定的是Flatten
的返回 Item 类型,而不是O
。 I was confused by the syntax and had some illogical understanding.我被语法弄糊涂了,有一些不合逻辑的理解。
Rust knows what item to chose because we specifically tell him what Item
to chose. Rust 知道要选择什么项目,因为我们特别告诉他要选择什么
Item
。 We know that both Iterator
and IntoIterator
define an Item
.我们知道
Iterator
和IntoIterator
定义了一个Item
。
So Flatten
is a collection of other IntoIterator
( O
) , being the target to access this IntoIterator inner items we need to set the Flatten::Item
to be the same as the inner IntoIterator::Item
, hence:所以
Flatten
是其他IntoIterator
( O
) 的集合,作为访问这个 IntoIterator 内部项目的目标,我们需要将Flatten::Item
设置为与内部IntoIterator::Item
,因此:
type Item = <O::Item as IntoIterator>::Item;
Since we are implementing Iterator
the Self::Item
is that implementation-defined item (the inner ones that we just explained).由于我们正在实现
Iterator
因此Self::Item
是实现定义的项目(我们刚刚解释的内部项目)。
As recap:回顾一下:
O::Item
=> Inner IntoIterator::Item
for flatten O::Item
=> Inner IntoIterator::Item
用于展平Self::Item
=> Flatten as Iterator Item
. Self::Item
=> Flatten as Iterator Item
。 Which matches the previous one.Flatten::Item
=> Same as Self::Item
for the Iterator
implementation. Flatten::Item
=> 与Iterator
实现的Self::Item
相同。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.