【发布时间】:2014-10-21 11:59:32
【问题描述】:
我正在尝试使用 JSON 接口编写一些 Rust。
我希望以下结构自动从 JSON 编码/解码:
#[deriving(Encodable)]
struct Joined<'a> {
channel: &'a str,
user: &'a str,
users: &'a [str],
}
示例 JSON:
{
"channel":"foo",
"user":"bar",
"users":[
"bar",
"baz",
"quux"
]
}
我得到的错误是:
src/chat.rs:28:5: 28:21 error: type `&[str]` does not implement any method in scope named `encode`
src/chat.rs:28 users: &'a [str],
^~~~~~~~~~~~~~~~
src/chat.rs:24:12: 24:21 note: in expansion of #[deriving(Encodable)]
src/chat.rs:28:5: 28:21 note: expansion site
我在 Rust 中的尝试是否可行,还是我误解了 Encodable 的本质?
【问题讨论】:
-
对结构字段使用
String,除非你有充分的理由使用切片。