Other Formats
TOML, YAML, bincode.
Other Formats is a free Learn Rust Coding lesson on CoddyKit — lesson 4 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.
Beyond JSON
serde's core is format-agnostic, so the same derived struct works with many formats just by swapping the format crate.
- TOML for config
- YAML for human-friendly data
- bincode for compact binary
One struct, many formats
You define the struct once. Each format crate provides its own to_string / from_str equivalents over the same Serialize / Deserialize impls.
use serde::{Serialize, Deserialize};
#[derive(Serialize, Deserialize)]
struct Config {
name: String,
port: u16,
}TOML
TOML is popular for config (Cargo.toml itself uses it). The toml crate serializes and parses it.
use serde::Serialize;
#[derive(Serialize)]
struct Config { name: String, port: u16 }
fn to_toml(c: &Config) -> String {
toml::to_string(c).unwrap()
}Parsing TOML
Read a TOML config back into your struct with toml::from_str.
use serde::Deserialize;
#[derive(Deserialize)]
struct Config { name: String, port: u16 }
fn parse(s: &str) -> Config {
toml::from_str(s).unwrap()
}YAML
YAML suits human-edited data with nesting. Use a crate such as serde_yaml with the same API shape.
use serde::Serialize;
#[derive(Serialize)]
struct Config { name: String, port: u16 }
fn to_yaml(c: &Config) -> String {
serde_yaml::to_string(c).unwrap()
}bincode
bincode is a compact binary format, great for caching or sending over the wire where size and speed matter and humans never read it.
use serde::Serialize;
#[derive(Serialize)]
struct Config { name: String, port: u16 }
fn to_bytes(c: &Config) -> Vec<u8> {
bincode::serialize(c).unwrap()
}Choosing a format
Pick based on the audience:
- Human config that nests: TOML or YAML
- Web APIs: JSON
- Internal binary storage/transport: bincode
Modeling format choice
The decision is just a mapping from use-case to format. This runnable example captures that selection logic.
fn pick_format(use_case: &str) -> &str {
match use_case {
"config" => "TOML",
"api" => "JSON",
"cache" => "bincode",
_ => "JSON",
}
}
fn main() {
println!("{}", pick_format("config"));
println!("{}", pick_format("cache"));
println!("{}", pick_format("api"));
}Round-tripping bytes
Binary formats round-trip: serialize to bytes, then deserialize back into the same struct. This runnable example models a byte-length round trip conceptually.
fn encode(name: &str) -> Vec<u8> {
name.as_bytes().to_vec()
}
fn decode(bytes: &[u8]) -> String {
String::from_utf8(bytes.to_vec()).unwrap()
}
fn main() {
let bytes = encode("Alice");
println!("len={}", bytes.len());
println!("back={}", decode(&bytes));
}Format-specific quirks
Formats differ in capability. TOML disallows null and needs tables for nesting; YAML supports anchors; bincode is not self-describing, so both sides must share the type.
Attributes still apply
The #[serde(...)] attributes you learned (rename, default, skip) work across all formats, since they operate at the serde layer, not the format layer.
use serde::Serialize;
#[derive(Serialize)]
#[serde(rename_all = "kebab-case")]
struct Config { max_connections: u32 }Quick Check
Test your understanding of other formats.
Recap
You learned about other serde formats:
- One derived struct works across TOML, YAML, JSON, and bincode
- Each format crate offers familiar
to_string/from_strstyle APIs - Choose by audience: config, API, or binary storage
- serde attributes apply to every format
That completes the Serde course and the Rust series.
Frequently asked questions
Is the “Other Formats” lesson free?
Yes — the full text of “Other Formats” 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 “Other Formats”?
TOML, YAML, bincode. 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 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Other Formats” 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.