Get length of csv iterator

Using the csv crate, how can I get the length of the iterator when reading a .csv?

let mut rdr = csv::Reader::from_reader(io::stdin());

let iter = rdr.deserialize();

iter is a csv::DeserializeRecordsIter<'_, std::fs::File, _> which doesn't have a len method. Is there no way to know up front how many rows are in the .csv?

No, counting the number of rows in a csv file requires reading the entire file and counting. You can do it using the .count() method on the iterator, but this consumes the iterator and you'll need to recreate it to iterate through the contents afterwards.

2 Likes

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.