Created
August 21, 2017 14:04
-
-
Save anonymous/63bcd00691b4bedee781c49435d0d729 to your computer and use it in GitHub Desktop.
Rust code shared from the playground
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#[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