我正在關注Iron web框架教程,它看起來很簡單,但我似乎無法將結構編碼爲JSON。無法在Rust中將結構編碼到JSON中
extern crate iron;
extern crate rustc_serialize;
use iron::prelude::*;
use iron::status;
use rustc_serialize::json;
struct Greeting {
msg: String,
}
fn main() {
fn hello_world(_: &mut Request) -> IronResult<Response> {
let greeting = Greeting { msg: "hello_world".to_string() };
let payload = json::encode(&greeting).unwrap();
// Ok(Response::with((status::Ok,payload)))
}
// Iron::new(hello_world).http("localhost:3000").unwrap();
}
我Cargo.toml
[package]
name = "iron_init"
version = "0.1.0"
authors = ["mazbaig"]
[dependencies]
iron = "*"
rustc-serialize = "*"
而且我的錯誤:
error: the trait bound `Greeting: rustc_serialize::Encodable` is not satisfied [E0277]
let payload = json::encode(&greeting).unwrap();
^~~~~~~~~~~~
help: run `rustc --explain E0277` to see a detailed explanation
note: required by `rustc_serialize::json::encode`
我還挺得到了正確的類型沒有得到傳遞到json.encode()
功能,但我無法弄清楚我想要什麼。我可能錯過了一些非常基本的東西。
哪個教程?你需要在'Greeting'上加'#[derive(RustcEncodable)]'' – kennytm