简体   繁体   English

对于必须实现“iter”函数的类型,我应该使用哪个特征绑定

[英]Which trait bound should I use for a type that must implement the "iter" function

I am trying to implement a struct composed of a generic field which type must implement the non-consuming "iter" method (see below).我正在尝试实现一个由泛型字段组成的结构,该类型必须实现非消耗的“iter”方法(见下文)。

struct Node<T> {
    key: T
}

impl<T> Node<T> where T: ?? {
    fn do_stuff(&self) {
        for e in self.key.iter() {
            /* ... */
        }
    }
}

fn main() {
    let n1 = Node { key: "foo".to_owned() };
    n1.do_stuff();
    
    let n2 = Node { key: vec![1, 2, 3] };
    n2.do_stuff();
}

Which trait bound should I associate with the parameter T ?我应该将哪个特征边界与参数T相关联?

The trait bound you're looking for is &T: IntoIterator .您正在寻找的 trait bound 是&T: IntoIterator As a convention, types that provide a non-consuming iter() also provide an implementation of IntoIterator for a reference to the type.作为惯例,提供非消费iter()还提供IntoIterator实现以引用该类型。 (Likewise, iter_mut() is equivalent to IntoIterator for &mut T .) (同样, iter_mut()等价于&mut T IntoIterator 。)

For example:例如:

impl<T> Node<T>
where
    for<'a> &'a T: IntoIterator,
{
    fn do_stuff(&self) {
        for e in &self.key { /* ... */ }
    }
}

Note, however, that this won't compile with key: "foo".to_owned() simply because a String is not iterable.但是请注意,这不会使用key: "foo".to_owned()编译,因为String不可迭代。

Which trait bound should I associate with the parameter T ?我应该将哪个特征边界与参数T相关联?

There is no such a trait in the standard library, the iter() is an inherent method – it doesn't come from a trait implementation.标准库中没有这样的特征, iter()是一个固有的方法——它不是来自特征实现。 You could, of course, create your own trait (eg, Iter ) that specifies iter() .当然,您可以创建自己的特征(例如, Iter )来指定iter()

If you are working with a generic type and need to ensure that such type's values can be turned into an iterator, you may want to consider bounding the type to the IntoIterator trait.如果您正在使用泛型类型并需要确保此类类型的值可以转换为迭代器,您可能需要考虑将类型IntoIteratorIntoIterator特征。

暂无
暂无

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

相关问题 为具有嵌套边界的类型实现特征 - Implement trait for type with nested bound 函数如何要求类型实现特征而不删除现有特征界限? - How can a function require that a type implement a trait without removing the existing trait bound? 我应该如何使用一个受我的特征绑定的通用参数为我的结构实现 From/Into? - How should I implement From/Into for my struct with one generic parameter bound by my trait? 如果在函数内创建引用,如何将泛型类型与需要使用生命周期参数的特征绑定在一起? - How do I bound a generic type with a trait that requires a lifetime parameter if I create the reference inside the function? 如何在另一个泛型类型的特征绑定的类型参数上表达特征? - How can I express a trait bound on a type parameter for another generic type's trait bound? 什么时候应该在Scala的通用类中使用类型绑定? - When should I use type bound in generic class in scala? 我如何实现具体的类,该类扩展了通过类型参数的类型别名定义类型的方法的特征 - How can I implement concrete class which extends trait defining a method with type by the type parameter's type alias 在方法中使用类型参数实现trait - Implement trait with type parameters in method 如何实现泛型类型的特征? - How to implement a trait for generic type? 如何在特征本身上写一个特征绑定来引用关联类型? - How to write a trait bound for a reference to an associated type on the trait itself?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM