Struct lines::linereader::LineReader
[−]
[src]
pub struct LineReader<R> { // some fields omitted }
Wraps a Read
and provides a way to iterate over lines split on a
newline character.
Unlike BufReaderExt::lines
a line provided by
LineReader::read_line
is represented as a slice into an internal
buffer which gets overwritten upon the next invocation of this
method. This allows clients to read through lines without
enforcing memory allocation on every line. On the other hand,
clients must copy the provided data if a line is to be kept for
later reference.
Examples
Iterating over all the lines of a given Read
can be implemented
as follows:
extern crate lines;
let r: Read = ...;
let mut lr = LineReader::new(r);
loop {
match lr.read_line() {
Ok(b) if b.is_empty() => { /* end of file */ }
Ok(line) => { /* process line bytes */ }
Err(e) => { /* i/o error occured */ }
}
}
For convenience, you'd usually use the read_lines
macro provided
by this module. The macro will loop over the lines and take care
of aborting the loop when encountering end-of-file.
#[macro_use(read_lines)]
extern crate lines;
let r: Read = ...;
let mut reader = LineReader::new(r);
read_lines(line in reader, {
match line {
Ok(line) => { /* process line bytes; line is never empty */ }
Err(e) => { /* i/o error occured */ }
}
});
Methods
impl<R: Read> LineReader<R>
[src]
fn with_capacity(cap: usize, inner: R) -> LineReader<R>
Constructs a new LineReader
with an internal buffering of
the specified capacity.
fn new(inner: R) -> LineReader<R>
Constructs a new LineReader
with an internal buffering of
the default capacity.
fn read_line(&mut self) -> Result<&[u8]>
Reads the next line. Returns the empty slice if EOF is reached. The newline character - if any - is not stripped by this method.