Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Tips and Traps

  1. Rust crates serde and ron are popular serialization/deserialization libraries.

Dependencies

:timing
:sccache 1
:dep serde = { version = "1.0", features = ["derive"] }
:dep serde_json = "1.0"

Serialization

use serde::{Serialize, Deserialize};
#[derive(Serialize, Deserialize, Debug)]
struct Point {
    x: i32,
    y: i32,
}
let point = Point { x: 1, y: 2 };
let s = serde_json::to_string(&point).unwrap();
s
"{\"x\":1,\"y\":2}"
println!("Point serialized = {}", s);
Point serialized = {"x":1,"y":2}

Deseriallization

let p2: Point = serde_json::from_str(&s).unwrap();
p2
Point { x: 1, y: 2 }
println!("Point deserialized = {:?}", p2);
Point deserialized = Point { x: 1, y: 2 }