Оverflow 评估需求,Rust Traits

发布于 2025-01-17 07:47:14 字数 811 浏览 2 评论 0 原文

我正在尝试写一个需要内部类型迭代器的特征。迭代应实现迭代器。我遇到了这样的错误“溢出评估需求 str as thereRait> :: iter == _ ”,并且无法理解它想要什么。有人可以帮助理解吗?)

pub trait SomeIter { 
   type Iter where Self::Iter: Iterator;
}

struct NumberIter(u32, u8);

impl Iterator for NumberIter {
    type Item = u8;

    fn next(&mut self) -> Option<Self::Item> {
        if self.1 == 32 || (self.0 >> self.1) == 0 {
            None
        } else {
            let ret = ((self.0 >> self.1) as u8) & 0xFF;
            self.1 += 8;
            Some(ret)
        }
    }
}

impl SomeTrait for str {
    type Iter = NumberIter;
}

编译器建议阅读一些示例,但没有类似的东西。

I am trying to write a trait which requires an inner type Iter. And Iter should implements Iterator. I am getting such error "overflow evaluating the requirement <str as SomeTrait>::Iter == _" and can't understand what it wants. Can anybody help to understand ?)

pub trait SomeIter { 
   type Iter where Self::Iter: Iterator;
}

struct NumberIter(u32, u8);

impl Iterator for NumberIter {
    type Item = u8;

    fn next(&mut self) -> Option<Self::Item> {
        if self.1 == 32 || (self.0 >> self.1) == 0 {
            None
        } else {
            let ret = ((self.0 >> self.1) as u8) & 0xFF;
            self.1 += 8;
            Some(ret)
        }
    }
}

impl SomeTrait for str {
    type Iter = NumberIter;
}

The compiler advised reading some examples in https://doc.rust-lang.org/error-index.html#E0275 but there weren't similar ones.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

东风软 2025-01-24 07:47:14

问题在于 where Self::Iter 部分的解析(因为它是递归的),您只需要限制类型:

pub trait SomeIter { 
   type Iter: Iterator;
}

游乐场

The problem is the resolution over the where Self::Iter part (because it is recursive), where you just really need to constrain the type:

pub trait SomeIter { 
   type Iter: Iterator;
}

Playground

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文