serde for zero-copy serialization and deserialization

Marcus Chen Jan 2026
1 tab
use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize, Debug)]
struct Config {
    host: String,
    port: u16,
    #[serde(default)]
    debug: bool,
}

fn main() -> Result<(), serde_json::Error> {
    let json = r#"{"host": "localhost", "port": 8080}"#;
    let config: Config = serde_json::from_str(json)?;
    println!("{:?}", config);
    Ok(())
}
1 file · rust Explain with highlit

Serde is Rust's serialization framework, supporting JSON, YAML, TOML, MessagePack, and more through format-specific crates. With #[derive(Serialize, Deserialize)], your structs automatically convert to and from these formats. Serde is extremely fast because it generates specialized code at compile time and can borrow from the input (&str fields) without copying. I use serde_json for APIs, serde_yaml for config files, and bincode for binary protocols. The #[serde(rename = "...")] attribute maps Rust field names to external formats. For custom serialization, you can implement the traits manually. The ecosystem is mature, well-documented, and integrates with async I/O seamlessly. It's essential for any networked Rust application.