如何从 Vector 创建非消耗迭代器 [英] How to create a non consuming iterator from a Vector

查看:34
本文介绍了如何从 Vector 创建非消耗迭代器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

情况:

我想在函数参数上调用在 Iterator trait 上定义的一些方法.我想调用它的函数采用一个类型为 trait 的参数,称为 VecLike.该函数称为get_all_matching_rules.

I have a situation where I would like to call some method defined on the Iterator trait on a function parameter. The function that I would like to call it is taking a parameter of a type which is a trait called VecLike. The function is called get_all_matching_rules.

get_all_matching_rules 可以接收Vec 或其他类似的自制 类型,它也实现了Iterator.当然,这两个都实现了VecLike.我想在 VecLike 上添加一个函数,让它返回一个 Iterator,这样我就可以在 get_all_matching_rules 中使用它.

get_all_matching_rules can receive either a Vec or another similar home made type which also implements Iterator. Of course both of these implement VecLike. I was thinking of adding a function on VecLike to have it return an Iterator so that I could use it in get_all_matching_rules.

如果我的参数被命名为:matching_rules 那么我可以执行 matching_rules.iter().filter(.

If my parameter is named: matching_rules I could then do matching_rules.iter().filter(.

问题:

如何从 Vec 返回非消耗迭代器?

How do I return a non consuming iterator from a Vec?

我希望能够在 Iterator 类型的 Vec 上返回一个非消耗迭代器.我不想通过调用 .iter() 来迭代项目.

I'd like to be able to return a non consuming iterator on a Vec<T> of type Iterator<T>. I am not looking to iterate the items by calling .iter().

如果我有(其中 self 是 Vec):

If I have (where self is a Vec):

fn iter<'a>(&'a self) -> Iterator<T> {
    self.iter()
}

我收到以下错误:

error: mismatched types: expected `core::iter::Iterator<T>+'a`, found `core::slice::Items<'_,T>` (expected trait core::iter::Iterator, found struct core::slice::Items)

我想返回Iterator.如果有更好的方法来解决这个问题而不是返回 Iterator,我会全力以赴.

I would like to return the Iterator<t>. If there is a better way to go at this rather than returning an Iterator, I'm all ears.

推荐答案

<[T] 上的 code>.iter(),Vec 自动取消引用,通过引用获取 self 并生成一个类型实现 Iterator<&T>.注意返回类型是not Iterator<&T>;Iterator 是一个由具体类型实现的 trait,具体类型 Items 在这种情况下是返回类型,而不是 <代码>迭代器<&T>.目前没有任何语法仅将返回类型指定为由它实现的特征,尽管已经建议使用语法 impl Iterator<&T>.

.iter() on [T], which Vec<T> automatically dereferences to, takes self by reference and produces a type implementing Iterator<&T>. Note that the return type is not Iterator<&T>; Iterator is a trait which is implemented by concrete types, and the concrete type Items<T> is the return type in that case, not Iterator<&T>. There is not currently any syntax for specifying a return type merely as a trait that is implemented by it, though the syntax impl Iterator<&T> has been suggested.

现在您希望实现 Iterator 而不是 Iterator<&T>.在 Rust 的内存模型中,每个对象都由一个事物拥有,这是不可能的对于相同的对象;必须有一些约束才能让您从 &T 中获取新的 T.为此,有两种现成的解决方案:

Now you wish something implementing Iterator<T> rather than Iterator<&T>. Under Rust’s memory model where each object is owned by exactly one thing, this is not possible with the same objects; there must be some constraint to allow you to get a new T from the &T. There are two readily provided solutions for this:

  1. Copy 特性,对于可以按位复制的类型.给定一个实现 Iterator<&T> 类型的变量,其中 TCopy,这可以写成 .map(|&x| x).map(|x| *x)(两者是等价的).

  1. The Copy trait, for types that can just be copied bitwise. Given a variable of a type implementing Iterator<&T> where T is Copy, this can be written .map(|&x| x) or .map(|x| *x) (the two are equivalent).

Clone 特性,对于无论 Copy 边界如何,可以使操作有意义的任何类型.给定一个实现 Iterator<&T> 类型的变量,其中 TClone,这可以写成 .map(|x| x.clone()).

The Clone trait, for any types where the operation can be caused to make sense, regardless of Copy bounds. Given a variable of a type implementing Iterator<&T> where T is Clone, this can be written .map(|x| x.clone()).

因此,给定一个向量 vv.iter().map(|x| x.clone()).一般是这样的:

Thus, given a vector v, v.iter().map(|x| x.clone()). Generically, something like this:

fn iter<T: Clone>(slice: &[T]) -> Map<&T, T, Items<T>> {
    slice.iter().map(|x| x.clone())
}

这篇关于如何从 Vector 创建非消耗迭代器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆