Skip to content

Instantly share code, notes, and snippets.

Created August 21, 2017 14:04
Show Gist options
  • Save anonymous/63bcd00691b4bedee781c49435d0d729 to your computer and use it in GitHub Desktop.
Save anonymous/63bcd00691b4bedee781c49435d0d729 to your computer and use it in GitHub Desktop.
Rust code shared from the playground
#[macro_use]
extern crate serde_derive;
extern crate serde;
extern crate serde_json;
#[derive(Serialize, Deserialize, Debug)]
struct Point {
x: i32,
y: i32,
}
#[derive(Serialize, Deserialize, Debug)]
struct TwoPoints {
fst: Point,
snd: Point,
}
fn main() {
let point = TwoPoints {
fst: Point { x: 1, y: 2 },
snd: Point { x: 2, y: 1 },
};
// Convert the Point to a JSON string.
let serialized = serde_json::to_string(&point).unwrap();
// Prints serialized = {"x":1,"y":2}
println!("serialized = {}", serialized);
// Convert the JSON string back to a Point.
let deserialized: TwoPoints = serde_json::from_str(&serialized).unwrap();
// Prints deserialized = Point { x: 1, y: 2 }
println!("deserialized = {:?}", deserialized);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment