gpt4 book ai didi

rust - 将范围作为结构域

转载 作者:行者123 更新时间:2023-12-03 11:44:26 25 4
gpt4 key购买 nike

有多种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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com