pub struct Reader<R: Read> { /* private fields */ }Expand description
A pull-based CSV reader that parses records from any Read source.
The reader supports both in-memory slices (&[u8]) and streaming I/O sources
(std::io::Read) via the custom Read trait. Rows are parsed on-demand
by calling rows or rows_bytes.
§Configuration
Use builder methods for a fluent setup:
set_delimiter— change the field separator (default,)set_flexible— allow variable-length rows (defaultfalse)set_headers— supply column names without parsing
§Headers
Call parse_headers to treat the first row as column
names. After doing so, headers returns a Some slice and
serde deserialization (with the serde feature) can match by field name.
§Errors
Parsing errors (unterminated quotes, trailing content after a quoted field, inconsistent field counts) are stored on the returned row and surfaced when the row’s fields are accessed. I/O errors are also stored on the row. This allows iteration to continue past malformed rows.
§Iteration
Produces owned Row or BytesRow values. A Row validates UTF-8
on field access; BytesRow does not. Rows can outlive the reader.
§Examples
use csv::Reader;
let data = b"name,age\nAlice,30\nBob,25\n";
let mut reader = Reader::new(&data[..]);
// Parse the first row as headers.
let headers = reader.parse_headers().unwrap();
assert_eq!(headers, &["name", "age"]);
// Iterate over data rows.
for row in reader.rows() {
for field in row.iter() {
let _: &str = field.unwrap();
}
}let file = std::fs::File::open("data.csv").unwrap();
let mut reader = Reader::new(file).set_delimiter(b';');
for row in reader.rows() {
let name = row.get(0).unwrap().unwrap();
println!("{name}");
}Implementations§
Source§impl<R: Read> Reader<R>
impl<R: Read> Reader<R>
Sourcepub fn new(source: R) -> Self
pub fn new(source: R) -> Self
Creates a new Reader from any Read source.
This works with &[u8], std::io::Cursor, std::fs::File, etc.
Sourcepub fn set_delimiter(self, byte: u8) -> Self
pub fn set_delimiter(self, byte: u8) -> Self
Sets the field delimiter byte (default is ,).
Sourcepub fn set_flexible(self, yes: bool) -> Self
pub fn set_flexible(self, yes: bool) -> Self
Sets whether variable field counts are allowed (default is false).
Sourcepub fn set_headers(self, headers: Vec<String>) -> Self
pub fn set_headers(self, headers: Vec<String>) -> Self
Sets the column names for header-based serde deserialization.
Calling this marks headers as parsed so reader.headers() returns Some.
Sourcepub fn headers(&self) -> Option<&[String]>
pub fn headers(&self) -> Option<&[String]>
Returns Some(&[String]) if headers were parsed or set, None otherwise.
Sourcepub fn parse_headers(&mut self) -> Result<&[String], ReadError>
pub fn parse_headers(&mut self) -> Result<&[String], ReadError>
Parses the first row as column headers and stores them internally.
Returns the header strings as a slice. Returns an empty slice if the CSV is empty.
After calling this, reader.headers() returns Some(...) and serde
deserialization matches struct fields by name.
Sourcepub fn rows(&mut self) -> Rows<'_, R> ⓘ
pub fn rows(&mut self) -> Rows<'_, R> ⓘ
Returns an iterator over Row values (validated as UTF-8).
Sourcepub fn rows_bytes(&mut self) -> BytesRows<'_, R> ⓘ
pub fn rows_bytes(&mut self) -> BytesRows<'_, R> ⓘ
Returns an iterator over BytesRow values (no UTF-8 validation).