- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个特性,我想说如果一个结构实现了这个特性,那么它也可以充当一个Iterator
。但是,当我尝试将结构用作迭代器时遇到编译器错误。
我正在编写一个库来从许多不同的文件格式中读取相同类型的数据。我想创建一个通用的“阅读器”特征,它将返回正确的使用rust 对象。我想说的是,每个读者都可以作为迭代器操作,生成该对象。
这是代码
/// A generic trait for reading u32s
trait MyReader {
fn get_next(&mut self) -> Option<u32>;
}
/// Which means we should be able to iterate over the reader, yielding u32s
impl Iterator for MyReader {
type Item = u32;
fn next(&mut self) -> Option<u32> {
self.get_next()
}
}
/// Example of a 'reader'
struct MyVec {
buffer: Vec<u32>,
}
/// This can act as a reader
impl MyReader for MyVec {
fn get_next(&mut self) -> Option<u32> {
self.buffer.pop()
}
}
fn main() {
// Create a reader
let mut veccy = MyVec { buffer: vec![1, 2, 3, 4, 5] };
// Doesn't work :(
let res = veccy.next();
}
编译器输出:
rustc 1.15.0 (10893a9a3 2017-01-19)
error: no method named `next` found for type `MyVec` in the current scope
--> <anon>:31:21
|
31 | let res = veccy.next();
| ^^^^
|
= help: items from traits can only be used if the trait is implemented and in scope; the following traits define an item `next`, perhaps you need to implement one of them:
= help: candidate #1: `std::iter::Iterator`
= help: candidate #2: `std::iter::ZipImpl`
= help: candidate #3: `std::str::pattern::Searcher`
Here是 Rust Playground 上的代码。
在我看来,既然 MyVec
实现了 MyReader
,那么它应该可以用作迭代器,因此我应该能够调用 .next()
就可以了。既然我已经实现了MyReader
,那么我应该免费得到一个Iterator
的实现,对吧? impl Iterator for ...
行显示 Iterator
在范围内,因此我无法理解错误的来源。
最佳答案
这条线并不像你想象的那样。
impl Iterator for MyReader {
这为 trait object 实现了 Iterator
我的阅读器
。您想要的是为每个还实现了 MyReader
的类型实现 Iterator
。不幸的是,由于一致性规则,这是不可能的。
在 Rust 中,您只能在定义特征的 crate 或定义要实现它的类型的 crate 中实现特征。 (对于泛型类型,事情要复杂一些,但这是基本思想。)在这种情况下,Iterator
是标准库中的一个特征,因此您无法任意实现它您未定义的类型。如果您考虑一下,这是有道理的,否则如果其中一种类型具有 Iterator
的预先存在的实现,您就会产生歧义 - 将使用哪个?
一个解决方案是将实现MyReader
的类型包装在一个新类型中,并在其上实现Iterator
。由于您自己定义了新类型,因此您可以自由地在其上实现 Iterator
。
关于rust - Trait 实现了 Iterator,但不能将实现我的 trait 的结构用作 Iterator,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42076770/
我有以下查询: SELECT I.InsuranceID FROM Insurance I INNER JOIN JobDetail JD ON I.AccountID = JD.AccountID
我想在 SwiftUI 布局中将此函数用作具有不可变值的模板,但得到错误 Result of call to 'padding' 未使用: func keys (padding: CGFloat, t
直到最近我才使用 View 的标签元素,此后发现了一些很酷的用途。我遇到了一个不寻常的问题,希望有人能回答。这可能比 Android 更通用,但我不确定。它与 Java 如何处理 Integer 类有
这个问题在这里已经有了答案: What is the purpose of the var keyword and when should I use it (or omit it)? (19 个回
我有以下脚本(见下文)。我有两个问题: 1.在 Knockoutjs 的上下文中,下面这行是什么意思? ko.observable(null); 2.如何调用这里尚未定义的函数: that.activ
Java 社区中是否存在一种使用 with-repect-to 在方法中使用多个返回的思想流派,如下所示: public SomeClass someMethod(int someValue) {
我一直在尝试为我的网站创建一个小型社交媒体栏。出于某种原因,我无法计算出我想用来创建 Sprite 的图像无法加载。我还应该提一下,我在背景图像不显示方面遇到过类似的问题。 HTML调用是这样的:
我正在尝试使用 std::pair 枚举值作为 unordered_map 容器的键,但我在定义自定义哈希函数时遇到困难。 我尝试了以下方法: // Enum and pair declaration
我正在学习 JS/JQuery 以及匿名函数和闭包。我见过这样的例子: $('.button').click(function(){ /* Animations */ /* Other
我正在尝试使用菜单列表来浏览我的应用程序。尽管应用程序和路由运行良好,但我使用这段代码在控制台中收到了一些警告: {props.itemList.map((item, index) =>(
我只是想创建一个简单的测试,我在其中使用 DelegateHandlers 来实例化一个 HttpClient 而无需引入 Asp.net Core 包。我有 2 个删除处理程序 Throttling
我是answering another question在这里,用户有一个 ListView与 ItemsSource包含 UserControls .我说我不会推荐它,并被问为什么。 这真的让我很惊
我安装了3.5.2和 3.5.3使用 pyenv 的版本。 # pyenv versions * system (set by /usr/local/pyenv/version) 3.5.2
我正在使用 android studio 制作统一插件,但这里有问题。一些 SDK 提供仅使用 AppcompatActivity 来制作 fragment 但我的MainActivity , 正是
我在 Laravel 中使用 whereHas 来构建查询: })->whereHas('results', function ($query) use ($issued, $mode, $reque
我有一个 5Gb .dat 文件(> 1000 万行)。每行的格式如 aaaa bb cccc0123 xxx kkkkkkkkkkkkkk或 aaaaabbbcccc01234xxxkkkkkkkk
我有一个消费者类,它采用 NSInputStream 作为参数,它将被异步处理,并且我想推送来自生产者类的数据,该生产者类要求它提供 NSOutputStream 作为其输出源。现在我如何设置一个缓冲
我正在尝试使用 ENVs在 Symfony2 中设置我的参数。标量值很简单,但我有一些参数是数组,我需要使用 ENV 以某种方式设置它们。 有问题的参数: parameters: redis.se
在我的类作业中,我已经成功地做到了这一点,但只是在非常简单的程序中。今天,我有一个更复杂的程序,在我将 DEBUG 定义为一个符号后,Eclipse 做了可怕的笨拙的事情,并且在我删除定义后这些可怕的
我目前有 2 个复选框类别、一个下拉列表和一个表单中的提交按钮。该按钮应保持“禁用”状态,直到选中 A 类的一个复选框和选中 B 类选项之一并选择选择列表中的一个选项。它适用于复选框(当我在没有列表的
我是一名优秀的程序员,十分优秀!