expression

Read CSV records with headers

Iterate data rows as fallible string records. The default header handling treats the first record as column names. This example uses external crates; see the staged Cargo manifest. Expected stdout (not executed): pen: 2 book: 3 Common mistake: Counting the header as a data row when using default settings.

Formal Syntax & Structure

rust syntaxrust
csv::Reader::from_reader(input); reader.records()

Working Example

Example Snippetrust
fn main() {    let mut r = csv::Reader::from_reader(&b"item,qty\npen,2\nbook,3\n"[..]);    for result in r.records() {        let row = result.unwrap();        println!("{}: {}", &row[0], &row[1]);    }}
Rust Reference