采取Rust试驾 . 到目前为止很有趣,但我不确定如何在这个实例中设置特征边界来做一些有用的事情 .
无法为T找到trait serialize :: serialize :: Encodable,std :: io :: IoError>的实现
在我看来,我需要在枚举 List<T: Encodable>
上设置一个界限 . 但是,当我尝试这个时,编译器有点不高兴 .
错误:枚举定义中不允许使用特征边界
所以我假设我必须把限制放在实现 impl<T:Encodable>
上,但得到了这个......
错误:类型参数数量错误:预期2但找到0
如果是这样的话,我会如何在Rust中做这样的事情?
extern crate serialize;
use serialize::{ json, Encodable };
#[deriving(Decodable, Encodable)]
pub enum List<T> {
Node(T, Box<List<T>>),
Nil
}
impl<T> List<T> {
fn to_json(&self) -> String {
json::Encoder::str_encode(self)
}
}
我工作时似乎工作正常,不要尝试封装编码,因为它知道int是可编码的...
let mut list: List<int> = Nil;
...
let encoded_string = json::Encoder::str_encode(&list);
println!("{}", encoded_string);
1 回答
目前,界限不能放在结构和枚举上 . 这可能会改变,但在此之前,
impl
是您定义这些约束的地方 .让我们看一下特质的定义:Encodable<S: Encoder<E>, E> .
S
和E
是它抱怨的东西,希望你定义它们 .现在让我们通过使用扩展该属性的
rustc --pretty expanded
编译代码来查看#[deriving(Encodable)]
的作用 .是的,这很麻烦 . 但它显示了
Encodable
实现和正在编写的边界 . 基本上,预期约束将由您以相同的方式编写:您不关心任何特定类型,只是您有一个编码器 . 这就是它归结为: