expression

Deserialize CSV records into structs

Read flat typed records using Serde; named fields are matched using headers. Invalid typed values are errors, not zeroes. This example uses external crates; see the staged Cargo manifest. Expected stdout (not executed): pen 2 book 3 Common mistake: Assuming arbitrary nested JSON-like structures map directly to flat CSV columns.

Formal Syntax & Structure

rust syntaxrust
reader.deserialize::<T>()

Working Example

Example Snippetrust
fn main() {    #[derive(serde::Deserialize)]    struct Sale { item: String, qty: u32 }    let mut r = csv::Reader::from_reader(&b"qty,item\n2,pen\n3,book\n"[..]);    for result in r.deserialize::<Sale>() {        let row = result.unwrap();        println!("{} {}", row.item, row.qty);    }}
Rust Reference