Skip to main content

Reader

Struct Reader 

Source
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:

§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>

Source

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.

Source

pub fn set_delimiter(self, byte: u8) -> Self

Sets the field delimiter byte (default is ,).

Source

pub fn set_flexible(self, yes: bool) -> Self

Sets whether variable field counts are allowed (default is false).

Source

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.

Source

pub fn headers(&self) -> Option<&[String]>

Returns Some(&[String]) if headers were parsed or set, None otherwise.

Source

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.

Source

pub fn rows(&mut self) -> Rows<'_, R>

Returns an iterator over Row values (validated as UTF-8).

Source

pub fn rows_bytes(&mut self) -> BytesRows<'_, R>

Returns an iterator over BytesRow values (no UTF-8 validation).

Auto Trait Implementations§

§

impl<R> Freeze for Reader<R>
where R: Freeze,

§

impl<R> RefUnwindSafe for Reader<R>
where R: RefUnwindSafe,

§

impl<R> Send for Reader<R>
where R: Send,

§

impl<R> Sync for Reader<R>
where R: Sync,

§

impl<R> Unpin for Reader<R>
where R: Unpin,

§

impl<R> UnsafeUnpin for Reader<R>
where R: UnsafeUnpin,

§

impl<R> UnwindSafe for Reader<R>
where R: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.