通过例子学习Rust

49.8.3 Encodable

serialize::Encodable is a trait implemented for types to make them encodable by the serialize module.

Encodable types can be serialized into JSON using json::encode.

Just like Decodable, Encodable can be automatically derived for a struct using #[deriving(Encodable)].

extern crate serialize; use serialize::{json, Encodable}; #[deriving(Encodable)] struct City { name: &'static str, // Latitude lat: f32, // Longitude lon: f32, } fn main() { for city in [ City { name: "São Paulo", lat: -23.55, lon: -46.633333 }, City { name: "Lima", lat: -12.043333, lon: -77.028333 }, City { name: "Santiago", lat: -33.45, lon: -70.666667 }, ].iter() { // `encode` encodes an `Encodable` implementor into a `String` println!("{}", json::encode(city)); } }