Serialize and Deserialize
Derive macros.
Serialize and Deserialize is a free Learn Rust Coding lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Learn Rust Coding learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
What is Serde?
Serde (SERialize/DEserialize) is Rust's framework for converting data structures to and from formats like JSON, YAML, and more.
- Format-agnostic core plus format crates
- Driven by derive macros
Adding serde
Add serde with the derive feature, plus a format crate like serde_json. This edits Cargo.toml.
[dependencies]
serde = { version = "1", features = ["derive"] }
serde_json = "1"Deriving the traits
Annotate a struct with #[derive(Serialize, Deserialize)] to make it convertible. Each field must itself be serializable.
use serde::{Serialize, Deserialize};
#[derive(Serialize, Deserialize)]
struct User {
id: u32,
name: String,
}Serialize means to a format
Serialize turns a value into bytes/text in some format. The struct does not know which format; the format crate decides.
Deserialize means from a format
Deserialize parses text/bytes back into a Rust value, validating types and required fields along the way.
Modeling serialize manually
To build intuition, here is a hand-written serializer for a simple struct. serde generates code like this for you.
struct User { id: u32, name: String }
fn serialize(u: &User) -> String {
format!("id={};name={}", u.id, u.name)
}
fn main() {
let u = User { id: 7, name: "Alice".to_string() };
println!("{}", serialize(&u));
}Modeling deserialize manually
And here is the reverse: parsing text back into the struct. serde's generated code does this robustly across formats.
struct User { id: u32, name: String }
fn deserialize(s: &str) -> User {
let parts: Vec<&str> = s.split(',').collect();
let id = parts[0].parse().unwrap();
let name = parts[1].to_string();
User { id, name }
}
fn main() {
let u = deserialize("42,Bob");
println!("{} {}", u.id, u.name);
}Deriving on enums
Enums work too. By default each variant serializes by its name, with any data nested inside.
use serde::{Serialize, Deserialize};
#[derive(Serialize, Deserialize)]
enum Shape {
Circle { radius: f64 },
Square { side: f64 },
}Nested structures
serde recurses automatically. A struct containing other derived structs or Vecs just works.
use serde::{Serialize, Deserialize};
#[derive(Serialize, Deserialize)]
struct Address { city: String }
#[derive(Serialize, Deserialize)]
struct Person {
name: String,
addresses: Vec<Address>,
}Serialize-only or Deserialize-only
You can derive just one trait. An API response type might be Serialize only; an incoming request type Deserialize only.
use serde::Serialize;
#[derive(Serialize)]
struct ApiResponse {
status: u16,
message: String,
}Derive requirements
Every field type must implement the trait you derive. Standard types and collections already do; custom types need their own derive.
use serde::Serialize;
#[derive(Serialize)]
struct Inner { x: i32 }
#[derive(Serialize)]
struct Outer { inner: Inner }Quick Check
Test your understanding of serialize and deserialize.
Recap
You learned the serde basics:
- serde is a format-agnostic (de)serialization framework
#[derive(Serialize, Deserialize)]generates the conversion code- Works on structs, enums, and nested types
- You can derive just one direction when appropriate
Next: working with JSON via serde_json.
Frequently asked questions
Is the “Serialize and Deserialize” lesson free?
Yes — the full text of “Serialize and Deserialize” is free to read here on the web, and the Learn Rust Coding course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Learn Rust Coding course, upgrade to CoddyKit PRO.
What will I learn in “Serialize and Deserialize”?
Derive macros. You practise Learn Rust Coding with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Learn Rust Coding?
No prior experience is required. Learn Rust Coding on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Serialize and Deserialize” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Learn Rust Coding lesson?
Yes. Every Learn Rust Coding lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Serialize and Deserialize
- Working with JSON
- Custom Serialization
- Other Formats