迭代一系列泛型类型 [英] Iterating over a range of generic type
本文介绍了迭代一系列泛型类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有特质
特质B {
类型索引:大小+复制;
fn bounds(& self) - > (自我::索引,自我::索引);
}
我想获得所有指数
es bounds
:
fn iterate< T: B>(它:& T){
let(low,high)= it.bounds();
for i in low..high {}
}
这赢得了'因为没有约束类型 T
可以远程结束,并且编译器说了这么多:
< pre class =lang-none prettyprint-override>
错误[E0277]:特征绑定`< T as B> :: Index:std :: iter :: Step`不满足
- > src / main.rs:8:5
|
8 |因为我在低..高{}
| ^^^^^^^^^^^^^^^^^^^^^特征`std :: iter :: Step`未实现`< T as B> :: Index`
|
= help:考虑添加一个`where< T as B> :: Index:std :: iter :: Step` bound
= note:required因为对std的impl的要求: :iter :: Iterator` for`std :: ops :: Range<< T as B> :: Index>`
我尝试添加步骤
绑定到索引
使用std :: iter :: Step;
特质B {
类型索引:大小+复制+步骤;
fn bounds(& self) - > (自我::索引,自我::索引);
}
但显然不稳定:
错误:使用不稳定的库特征'step_trait':可能被更细粒度的特征所取代(参见问题#42168)
- > src / main.rs:1:5
|
1 |使用std :: iter :: Step;
| ^^^^^^^^^^^^^^^
错误:使用不稳定的库特征'step_trait':可能被更细粒度的特征所取代(参见问题#42168)
- > src / main.rs:4:32
|
4 |类型索引:大小+复制+步骤;
| ^^^^
我错过了什么或者现在是不可能这样做的?
解决方案
如果你想要一个范围< T>
可以迭代过来,只需使用它作为你的特征界限:
trait有界{
类型索引:大小+复制;
fn bounds(& self) - > (自我::索引,自我::索引);
}
fn iterate< T>(it:& T)
其中
T:Bounded,
std :: ops :: Range< T :: Index>:IntoIterator,
{
let(low,high)= it.bounds();
for i in low..high {}
}
fn main(){}
I have a trait
trait B {
type Index: Sized + Copy;
fn bounds(&self) -> (Self::Index, Self::Index);
}
I want to get all the Index
es within bounds
:
fn iterate<T: B>(it: &T) {
let (low, high) = it.bounds();
for i in low..high {}
}
This won't work since there's no constraint that the type T
can be "ranged" over, and the compiler says as much:
error[E0277]: the trait bound `<T as B>::Index: std::iter::Step` is not satisfied
--> src/main.rs:8:5
|
8 | for i in low..high {}
| ^^^^^^^^^^^^^^^^^^^^^ the trait `std::iter::Step` is not implemented for `<T as B>::Index`
|
= help: consider adding a `where <T as B>::Index: std::iter::Step` bound
= note: required because of the requirements on the impl of `std::iter::Iterator` for `std::ops::Range<<T as B>::Index>`
I tried adding the Step
bound to Index
use std::iter::Step;
trait B {
type Index: Sized + Copy + Step;
fn bounds(&self) -> (Self::Index, Self::Index);
}
but apparently it isn't stable:
error: use of unstable library feature 'step_trait': likely to be replaced by finer-grained traits (see issue #42168)
--> src/main.rs:1:5
|
1 | use std::iter::Step;
| ^^^^^^^^^^^^^^^
error: use of unstable library feature 'step_trait': likely to be replaced by finer-grained traits (see issue #42168)
--> src/main.rs:4:32
|
4 | type Index: Sized + Copy + Step;
| ^^^^
Am I missing something or is it just not possible to do so right now?
解决方案
If you want to require that a Range<T>
can be iterated over, just use that as your trait bound:
trait Bounded {
type Index: Sized + Copy;
fn bounds(&self) -> (Self::Index, Self::Index);
}
fn iterate<T>(it: &T)
where
T: Bounded,
std::ops::Range<T::Index>: IntoIterator,
{
let (low, high) = it.bounds();
for i in low..high {}
}
fn main() {}
这篇关于迭代一系列泛型类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文