在Rust 1.15中,我创建了一个trait来抽象读取和解析文件格式。我正在尝试创建一个包含这个泛型trait的结构体。
这是我的trait:
我想创建一个结构体,其中包含对实现此功能的某些内容的引用。
产生以下错误:
我该如何创建
这是我的trait:
use std::io::Read;
trait MyReader<R: Read> {
fn new(R) -> Self;
fn into_inner(self) -> R;
fn get_next(&mut self) -> Option<u32>;
fn do_thingie(&mut self);
}
我想创建一个结构体,其中包含对实现此功能的某些内容的引用。
struct MyIterThing<'a, T: MyReader<R>+'a> {
inner: &'a mut T,
}
产生以下错误:
error[E0412]: type name `R` is undefined or not in scope
--> <anon>:11:36
|
11 | struct MyIterThing<'a, T: MyReader<R>+'a> {
| ^ undefined or not in scope
|
= help: no candidates by the name of `R` found in your project; maybe you misspelled the name or forgot to import an external crate?
T: MyReader+'a
,我收到了错误信息:"error[E0243]: wrong number of type arguments: expected 1, found 0"
,T: MyReader<R: Read>+'a
会产生语法错误,它不希望在那里出现 :
。
而这个也不起作用:
error[E0392]: parameter `R` is never used
--> <anon>:11:24
|
11 | struct MyIterThing<'a, R: Read, T: MyReader<R>+'a> {
| ^ unused type parameter
|
= help: consider removing `R` or using a marker such as `std::marker::PhantomData`
我该如何创建
MyIterThing
结构体?