Trait BufReadExt
trait BufReadExt: io::BufRead
An extension trait for
std::io::BufRead
which provides convenience APIs for dealing with byte strings.
Provided Methods
fn byte_lines(self: Self) -> ByteLines<Self> where Self: SizedReturns an iterator over the lines of this reader, where each line is represented as a byte string.
Each item yielded by this iterator is a
io::Result<Vec<u8>>, where an error is yielded if there was a problem reading from the underlying reader.On success, the next line in the iterator is returned. The line does not contain a trailing
\nor\r\n.Examples
Basic usage:
use io; use BufReadExt; # ; example.unwrapfn byte_records(self: Self, terminator: u8) -> ByteRecords<Self> where Self: SizedReturns an iterator over byte-terminated records of this reader, where each record is represented as a byte string.
Each item yielded by this iterator is a
io::Result<Vec<u8>>, where an error is yielded if there was a problem reading from the underlying reader.On success, the next record in the iterator is returned. The record does not contain its trailing terminator.
Note that calling
byte_records(b'\n')differs frombyte_lines()in that it has no special handling for\r.Examples
Basic usage:
use io; use BufReadExt; # ; example.unwrapfn for_byte_line<F>(self: &mut Self, for_each_line: F) -> Result<()> where Self: Sized, F: FnMut(&[u8]) -> Result<bool>Executes the given closure on each line in the underlying reader.
If the closure returns an error (or if the underlying reader returns an error), then iteration is stopped and the error is returned. If false is returned, then iteration is stopped and no error is returned.
The closure given is called on exactly the same values as yielded by the
byte_linesiterator. Namely, lines do not contain trailing\nor\r\nbytes.This routine is useful for iterating over lines as quickly as possible. Namely, a single allocation is reused for each line.
Examples
Basic usage:
use io; use BufReadExt; # ; example.unwrapfn for_byte_record<F>(self: &mut Self, terminator: u8, for_each_record: F) -> Result<()> where Self: Sized, F: FnMut(&[u8]) -> Result<bool>Executes the given closure on each byte-terminated record in the underlying reader.
If the closure returns an error (or if the underlying reader returns an error), then iteration is stopped and the error is returned. If false is returned, then iteration is stopped and no error is returned.
The closure given is called on exactly the same values as yielded by the
byte_recordsiterator. Namely, records do not contain a trailing terminator byte.This routine is useful for iterating over records as quickly as possible. Namely, a single allocation is reused for each record.
Examples
Basic usage:
use io; use BufReadExt; # ; example.unwrapfn for_byte_line_with_terminator<F>(self: &mut Self, for_each_line: F) -> Result<()> where Self: Sized, F: FnMut(&[u8]) -> Result<bool>Executes the given closure on each line in the underlying reader.
If the closure returns an error (or if the underlying reader returns an error), then iteration is stopped and the error is returned. If false is returned, then iteration is stopped and no error is returned.
Unlike
for_byte_line, the lines given to the closure do include the line terminator, if one exists.This routine is useful for iterating over lines as quickly as possible. Namely, a single allocation is reused for each line.
This is identical to
for_byte_record_with_terminatorwith a terminator of\n.Examples
Basic usage:
use io; use BufReadExt; # ; example.unwrapfn for_byte_record_with_terminator<F>(self: &mut Self, terminator: u8, for_each_record: F) -> Result<()> where Self: Sized, F: FnMut(&[u8]) -> Result<bool>Executes the given closure on each byte-terminated record in the underlying reader.
If the closure returns an error (or if the underlying reader returns an error), then iteration is stopped and the error is returned. If false is returned, then iteration is stopped and no error is returned.
Unlike
for_byte_record, the lines given to the closure do include the record terminator, if one exists.This routine is useful for iterating over records as quickly as possible. Namely, a single allocation is reused for each record.
Examples
Basic usage:
use io; use ; # ; example.unwrap
Implementors
impl<B: io::BufRead> BufReadExt for B