Skip to main content

Reader

Struct Reader 

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

Parses CSV data from a byte slice or a std::io::Read source.

Create an iterator over rows with Reader::rows:

let data = b"name,age\nAlice,30\nBob,25\n";
for row in Reader::new(data).rows() {
    let row = row?;
    // ...
}

Implementations§

Source§

impl Reader

Source

pub fn new(data: &[u8]) -> Self

Create a reader from a byte slice. The data is copied into an internal buffer. This constructor is available with or without the std feature.

Source

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

Set the field delimiter byte (default is ,).

Source

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

Allow variable numbers of fields per row (default false).

Source

pub fn rows(self) -> Rows

Read all rows from this CSV source.

Consumes the reader and returns an iterator yielding Result<Row, ReadError>.

§Example
let data = b"a,b,c\n1,2,3\n";
let mut total = 0usize;
for row in Reader::new(data).rows() {
    total += row?.len();
}
assert_eq!(total, 6);
Source§

impl Reader

Source

pub fn from_reader(reader: impl Read + 'static) -> Self

Create a reader from any std::io::Read source. Data is streamed in chunks as rows are read, without loading the entire input.

§Example
let file = File::open("data.csv").unwrap();
for row in Reader::from_reader(file).rows() {
    let row = row?;
    // ...
}

Auto Trait Implementations§

§

impl !RefUnwindSafe for Reader

§

impl !Send for Reader

§

impl !Sync for Reader

§

impl !UnwindSafe for Reader

§

impl Freeze for Reader

§

impl Unpin for Reader

§

impl UnsafeUnpin for Reader

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.