有多种Range
类型。一些Range
类型实现Iterator
。我想将实现Range
的所有Iterator
类型作为结构字段。
这是我的方法:
pub trait RangeBoundsExt<T: PartialOrd<T>>: Iterator<Item = T> {
// some methods
}
impl<T: PartialOrd<T>> RangeBoundsExt<T> for std::ops::Range<T> {}
impl<T: PartialOrd<T>> RangeBoundsExt<T> for std::ops::RangeFrom<T> {}
impl<T: PartialOrd<T>> RangeBoundsExt<T> for std::ops::RangeInclusive<T> {}
pub struct Foo<T> {
range: Box<dyn RangeBoundsExt<T>>
}
Playground但我收到此错误:
Compiling playground v0.0.1 (/playground)
error[E0277]: the trait bound `T: std::iter::Step` is not satisfied
--> src/lib.rs:7:24
|
7 | impl<T: PartialOrd<T>> RangeBoundsExt<T> for std::ops::Range<T> {}
| ^^^^^^^^^^^^^^^^^ the trait `std::iter::Step` is not implemented for `T`
|
= note: required because of the requirements on the impl of `std::iter::Iterator` for `std::ops::Range<T>`
help: consider further restricting this bound
|
7 | impl<T: PartialOrd<T> + std::iter::Step> RangeBoundsExt<T> for std::ops::Range<T> {}
| ^^^^^^^^^^^^^^^^^
error[E0277]: the trait bound `T: std::iter::Step` is not satisfied
--> src/lib.rs:9:24
|
9 | impl<T: PartialOrd<T>> RangeBoundsExt<T> for std::ops::RangeFrom<T> {}
| ^^^^^^^^^^^^^^^^^ the trait `std::iter::Step` is not implemented for `T`
|
= note: required because of the requirements on the impl of `std::iter::Iterator` for `std::ops::RangeFrom<T>`
help: consider further restricting this bound
|
9 | impl<T: PartialOrd<T> + std::iter::Step> RangeBoundsExt<T> for std::ops::RangeFrom<T> {}
| ^^^^^^^^^^^^^^^^^
error[E0277]: the trait bound `T: std::iter::Step` is not satisfied
--> src/lib.rs:11:24
|
11 | impl<T: PartialOrd<T>> RangeBoundsExt<T> for std::ops::RangeInclusive<T> {}
| ^^^^^^^^^^^^^^^^^ the trait `std::iter::Step` is not implemented for `T`
|
= note: required because of the requirements on the impl of `std::iter::Iterator` for `std::ops::RangeInclusive<T>`
help: consider further restricting this bound
|
11 | impl<T: PartialOrd<T> + std::iter::Step> RangeBoundsExt<T> for std::ops::RangeInclusive<T> {}
| ^^^^^^^^^^^^^^^^^
error: aborting due to 3 previous errors
For more information about this error, try `rustc --explain E0277`.
error: could not compile `playground`.
To learn more, run the command again with --verbose.
最佳答案
Range
不保证Iterator
实现。如果该类型实现Step
,则仅提供一个。同样,RangeTo
不能保证缺少Iterator
实现。默认情况下,它只是不提供一个。要解决您的错误,您只需要要求该范围的迭代器定义即可:
// note the additional 'where' requirement
impl<T: PartialOrd<T>> RangeBoundsExt<T> for std::ops::Range<T> where
std::ops::Range<T>: Iterator<Item = T>
{
}
impl<T: PartialOrd<T>> RangeBoundsExt<T> for std::ops::RangeFrom<T> where
std::ops::RangeFrom<T>: Iterator<Item = T>
{
}
impl<T: PartialOrd<T>> RangeBoundsExt<T> for std::ops::RangeInclusive<T> where
std::ops::RangeInclusive<T>: Iterator<Item = T>
{
}
关于rust - 将范围作为结构域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64739239/