$120 tested Claude codes · real before/after data · Full tier $15 one-timebuy --sheet=15 →
$Free 40-page Claude guide — setup, 120 prompt codes, MCP servers, AI agents. download --free →
clskills.sh — terminal v2.4 — 2,347 skills indexed● online
[CL]Skills_
RustbeginnerNew

Rust Serde

Share

Serialize and deserialize data with Serde in Rust

Works with OpenClaude

You are a Rust developer. The user wants to serialize and deserialize data structures using Serde, Rust's most popular serialization framework.

What to check first

  • Verify serde and serde_json are in Cargo.toml under [dependencies] with derive feature enabled
  • Run cargo build to confirm dependencies compile without errors

Steps

  1. Add serde with derive feature and serde_json to Cargo.toml: serde = { version = "1.0", features = ["derive"] } and serde_json = "1.0"
  2. Import Serialize and Deserialize traits: use serde::{Serialize, Deserialize};
  3. Derive both traits on your struct or enum with #[derive(Serialize, Deserialize, Debug)]
  4. Use serde_json::to_string() to serialize a struct instance into a JSON string
  5. Use serde_json::from_str() with type annotation to deserialize a JSON string back into your struct
  6. Handle Result types — both functions return Result<T, Error> so use .unwrap(), ?, or .expect() for error handling
  7. For custom serialization logic, use #[serde(rename)] for field names or implement custom serialize and deserialize methods with #[serde(serialize_with = "...")]
  8. Test round-trip serialization: serialize, then deserialize, and verify the data matches

Code

use serde::{Serialize, Deserialize};
use serde_json;

#[derive(Serialize, Deserialize, Debug)]
struct Person {
    name: String,
    age: u32,
    email: String,
}

#[derive(Serialize, Deserialize, Debug)]
struct Team {
    name: String,
    members: Vec<Person>,
}

fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Create a struct instance
    let person = Person {
        name: "Alice".to_string(),
        age: 30,
        email: "alice@example.com".to_string(),
    };

    // Serialize to JSON string
    let json_string = serde_json::to_string(&person)?;
    println!("Serialized: {}", json_string);
    // Output: Serialized: {"name":"Alice","age":30,"email":"alice@example.com"}

    // Deserialize from JSON string
    let deserialized: Person = serde_json::from_str(&json_string)?;
    println!("Deserialized: {:?}", deserialized);

    // Pretty-print JSON
    let pretty_json = serde_json::to_string_pretty(&person)?;
    println!("Pretty: {}", pretty_json);

    // Serialize complex nested structures

Note: this example was truncated in the source. See the GitHub repo for the latest full version.

Common Pitfalls

  • Treating this skill as a one-shot solution — most workflows need iteration and verification
  • Skipping the verification steps — you don't know it worked until you measure
  • Applying this skill without understanding the underlying problem — read the related docs first

When NOT to Use This Skill

  • When a simpler manual approach would take less than 10 minutes
  • On critical production systems without testing in staging first
  • When you don't have permission or authorization to make these changes

How to Verify It Worked

  • Run the verification steps documented above
  • Compare the output against your expected baseline
  • Check logs for any warnings or errors — silent failures are the worst kind

Production Considerations

  • Test in staging before deploying to production
  • Have a rollback plan — every change should be reversible
  • Monitor the affected systems for at least 24 hours after the change

Quick Info

CategoryRust
Difficultybeginner
Version1.0.0
AuthorClaude Skills Hub
rustserdeserialization

Install command:

curl -o ~/.claude/skills/rust-serde.md https://claude-skills-hub.vercel.app/skills/rust/rust-serde.md

Related Rust Skills

Other Claude Code skills in the same category — free to download.

Want a Rust skill personalized to YOUR project?

This is a generic skill that works for everyone. Our AI can generate one tailored to your exact tech stack, naming conventions, folder structure, and coding patterns — with 3x more detail.