expression

Project CSV records into a smaller output schema

A CLI projection emits selected columns while keeping CSV quoting correct. The fixture has a known two-column header; reject schema mismatches in a real tool. Use a CSV parser for quoted delimiters and embedded newlines instead of splitting on commas. Fixture: Expected stdout (not executed): name "red,blue" oak Expected stderr: (empty) Expected exit: 0 Common mistake: Splitting each line at a comma and corrupting quoted values. CSV quoting alone is not protection against spreadsheet formula interpretation.

Formal Syntax & Structure

rust syntaxrust
writer.write_record([&row[0]])?

Working Example

Example Snippetrust
fn main() -> Result<(), Box<dyn std::error::Error>> {    let mut reader = csv::Reader::from_reader("name,count\n\"red,blue\",2\noak,3\n".as_bytes());    assert_eq!(reader.headers()?.iter().collect::<Vec<_>>(), vec!["name", "count"]);    let mut writer = csv::Writer::from_writer(std::io::stdout().lock());    writer.write_record(["name"])?;    for row in reader.records() {        let row = row?;        writer.write_record([&row[0]])?;    }    writer.flush()?;    Ok(())}
Rust Reference