[Newbie] CSV Errors with using serde

Hi everyone, I just started learning Rust and I am running into problems trying to parse and print my csv. I get an error that says

Error: Error(Deserialize { pos: Some(Position { byte: 9, line: 2, record: 1 }), err: DeserializeError { field: None, kind: Message("missing field question") } })

I checked my CSV file and it is printing all of the records in the CSV. Any help or feedback would be appreciated.

use std::env;
use std::fs;
use csv::Error;
use serde::Deserialize;

struct Problem {
    question: String,
    answer: String,

fn main() -> Result<(), Error> {
    // Get a handle on the arguments from the command line
    // args[1] would be the argument given to the application 
    let args: Vec<String> = env::args().collect();
    let csv_filename = &args[1];

    //println!("{:?}", args);   
    let content = fs::read_to_string(csv_filename)
        .expect("Something went wrong with accessing the file");
    println!("{}", content);
    let mut reader = csv::Reader::from_reader(content.as_bytes());
    for record in reader.deserialize() {
        let record: Problem = record?;
        println!("{} {}",record.question, record.answer);


This is just a shot in the dark, but perhaps it's erroring on a blank line?

Can you provide an example of a failing input file?

Thank you for your response ...

Here is a link to the repo Quiz_Game Repo that also has a simple csv file.

I did try with another file and still getting the same error.

Your csv file doesn't have headers. Try this:

    let mut reader = csv::ReaderBuilder::new()

    for result in reader.deserialize() {
        let record: Problem = result?;
        println!("{:?}", record);

See also.


Thank you very much that solved the problem.


This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.