简体   繁体   English

实现 Iterator trait 的嵌套结构中的 Item 是什么?

[英]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 . IteratorItem ,所以我们可以写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 .我们知道IteratorIntoIterator定义了一个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.

相关问题 Trait实现了Iterator,但是不能使用实现我的trait的结构作为Iterator - Trait implements Iterator, but cannot use a struct implementing my trait as an Iterator Iterator特性中的type Item是什么意思 - What does `type Item` in the `Iterator` trait mean 在实现指定特征的结构中添加成员 - Add a member in a struct that implements a trait specified 为字段创建具有通用特征的结构。 预期结构 <trait> 找到结构 <type that implements said trait> - Creating a struct with a generic trait for field. Expected struct<trait> found struct<type that implements said trait> 无法将实现特征的结构装箱为特征 object - Can't box a struct that implements a trait as a trait object 为包含可迭代字段的结构实现迭代器特征 - Implement Iterator trait for a struct containing an iterable field 如何从实现Drop trait的结构中移出一个字段? - How to move one field out of a struct that implements Drop trait? 何时使用引用或框包含在结构中实现特征的字段? - When to use a reference or a box to have a field that implements a trait in a struct? 特质未实现(实现特质的事物) - Trait not implemented for (thing that implements trait) 为什么即使它实现了 IntoIterator,对于泛型类型的引用,我也会收到错误“特征 `Iterator` 未实现”? - Why do I get the error “the trait `Iterator` is not implemented” for a reference to a generic type even though it implements IntoIterator?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM