expression
Serialize typed CSV rows
For simple structs the default writer emits field names as a header and serializes subsequent values as records. Keep a consistent row schema. This example uses external crates; see the staged Cargo manifest. Expected stdout (not executed): name,qty pen,2 book,3 Common mistake: Changing struct fields halfway through one CSV output.
Formal Syntax & Structure
rust syntaxrust
writer.serialize(value)Working Example
Example Snippetrust
fn main() { #[derive(serde::Serialize)] struct Row { name: &'static str, qty: u32 } let mut w = csv::Writer::from_writer(Vec::new()); w.serialize(Row { name: "pen", qty: 2 }).unwrap(); w.serialize(Row { name: "book", qty: 3 }).unwrap(); let bytes = w.into_inner().unwrap(); print!("{}", String::from_utf8(bytes).unwrap());}