Skip to main content

Reader

Struct Reader 

Source
pub struct Reader<R> { /* private fields */ }
Expand description

Reads CSV data, either from a byte slice ([from_bytes]) or from any std::io::Read source ([from_reader], requires std feature).

Rows are yielded by calling rows, which returns an iterator of Row<'_>. Errors are deferred to the Row methods.

§Example

use csv::Reader;
use std::fs::File;

let mut reader = Reader::from_reader(File::open("data.csv")?);
for row in reader.rows() {
    for field in row.fields()? {
    }
}

§Headers

Use parse_headers to read the first row as column headers.

Implementations§

Source§

impl<R> Reader<R>

Source

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

Set the field delimiter byte (default is ,).

Source

pub fn set_headers(&mut self, headers: Vec<String>) -> &mut Self

Set column headers manually.

Use this when the CSV file has no header row but you still want Row::deserialize to map struct fields by column name.

Must be called before iterating with rows.

use csv::Reader;
let mut reader = Reader::from_reader(std::io::Cursor::new(b"Alice,30\nBob,25\n"));
reader.set_headers(vec!["name".into(), "age".into()]);
for row in reader.rows() {
    // row.deserialize::<MyStruct>() will match name/age by header
}
Source

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

Return the column headers, if parse_headers or set_headers was called.

Source§

impl<R: Read> Reader<R>

Source

pub fn from_reader(source: R) -> Self

Create a reader from any std::io::Read source.

Data is streamed in chunks as rows are parsed, without loading the entire input into memory.

Source

pub fn parse_headers(&mut self) -> Result<Vec<String>, ReadError>

Read and return the first row as column headers.

If the CSV has no header row, use set_headers instead.

Source

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

Return an iterator over the remaining rows.

Source§

impl<'a> Reader<&'a [u8]>

Source

pub fn from_bytes(bytes: &'a [u8]) -> Self

Create a reader from a byte slice for in-memory parsing.

Unlike [from_reader], this does not require std and copies the input into an internal buffer eagerly.

use csv::Reader;
let mut reader = Reader::from_bytes(b"a,b\n1,2\n");
let fields: Vec<Vec<String>> = reader.rows().map(|r| r.all().unwrap()).collect();
assert_eq!(fields, vec![vec!["a".to_string(), "b".to_string()], vec!["1".to_string(), "2".to_string()]]);

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.