简体   繁体   English

为什么 IntoIterator 特征需要显式指定关联类型 Item?

[英]Why does the IntoIterator trait require explicitly specifying the associated type Item?

Since the associated type IntoIter of the IntoIterator trait, implements the Iterator trait, isn't that sufficient to infer the associated type Item ?由于IntoIterator特征的关联类型IntoIter实现了Iterator特征,这还不足以推断关联类型Item吗?

Why does the IntoIterator trait require explicit type Item declaration?为什么IntoIterator特征需要显式type Item声明?

It doesn't .没有 You are correct that when you impl IntoIterator for... then Item is redundant , and could be obtained through IntoIter .你是对的,当你impl IntoIterator for...那么Item多余的,可以通过IntoIter获得。


This was introduced in PR #22313 .这是在PR #22313中介绍的。 In short, the reason is was introduced was to simplify where clauses.简而言之,引入的原因是为了简化 where 子句。 If you have to specify IntoIter then that can quickly become cumbersome.如果您必须指定IntoIter ,那么这很快就会变得很麻烦。

where I: IntoIterator<IntoIter = ...>

In that case it is much easier to do:在这种情况下,这样做要容易得多:

where I: IntoIterator<Item = ...>

Let's consider a random example, like print_strings .让我们考虑一个随机的例子,比如print_strings Then before you needed to do something like this:然后在你需要做这样的事情之前:

fn print_strings<I, T>(iter: I)
where
    I: IntoIterator<IntoIter = T>,
    T: Iterator<Item = &'static str>,
{
    for s in iter {
        println!("{}", s);
    }
}

Whereas now that can be simplified to just:而现在可以简化为:

fn print_strings<I>(iter: I)
where
    I: IntoIterator<Item = &'static str>,
{
    for s in iter {
        println!("{}", s);
    }
}

fn main() {
    print_strings(vec!["foo", "bar", "baz"]);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 Iterator trait 中指定关联类型的生命周期 - Specifying lifetime of the associated type in the Iterator trait rust:定义一个返回非消耗迭代器的特征。 (T impls IntoIterator 和 &amp;T impls IntoIterator 的关联类型界限) - rust: Define a trait that returns a non consuming iterator. (associated type bounds for both T impls IntoIterator and &T impls IntoIterator) 指定从其他特征继承的特征中的关联类型 - Specifying associated type in trait that inherits from another trait 如何将迭代器适配器与返回 impl Trait 作为 IntoIter 关联类型的 IntoIterator 的函数一起使用? - How do I use a iterator adapter with a function returning impl Trait as the IntoIter associated type of IntoIterator? 为什么 Rust 编译器需要 Option&lt;&amp;impl Trait&gt; 的类型注释? - Why does the rust compiler require a type annotation for Option<&impl Trait>? 为什么借用特征要求借用的类型作为参考? - Why does the Borrow trait require the borrowed type to be a reference? 为什么向 trait 添加泛型类型会影响 trait 对象和关联类型的生命周期? - Why does adding a generic type to a trait affect the lifetime of trait objects and associated types? 为什么即使它实现了 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? 为什么本地类型的 IntoIterator 实现与核心板条箱中的实现冲突? - Why does an implementation of IntoIterator for a local type clash with the one in the core crate? 如何指定关联类型<item=...>当调用一个特征的关联 function 时?</item=...> - How to specify associated type <Item=...> when calling associated function of a trait?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM