Function uucore::lines::lines

source ·
pub fn lines<B>(reader: B, sep: u8) -> Lines<B> where
    B: BufRead,
Expand description

Returns an iterator over the lines, including line ending characters.

This function is just like BufRead::lines, but it includes the line ending characters in each yielded String if the input data has them. Set the sep parameter to the line ending character; for Unix line endings, use b'\n'.

Examples

Use sep to specify an alternate character for line endings. For example, if lines are terminated by the null character b'\0':

use std::io::BufRead;
use std::io::Cursor;

let cursor = Cursor::new(b"x\0y\0z\0");
let mut it = lines(cursor, b'\0').map(|l| l.unwrap());

assert_eq!(it.next(), Some(Vec::from("x\0")));
assert_eq!(it.next(), Some(Vec::from("y\0")));
assert_eq!(it.next(), Some(Vec::from("z\0")));
assert_eq!(it.next(), None);

If the input data does not end with a newline character ('\n'), then the last String yielded by this iterator also does not end with a newline:

let cursor = Cursor::new(b"x\ny\nz");
let mut it = lines(cursor, b'\n').map(|l| l.unwrap());

assert_eq!(it.next(), Some(Vec::from("x\n")));
assert_eq!(it.next(), Some(Vec::from("y\n")));
assert_eq!(it.next(), Some(Vec::from("z")));
assert_eq!(it.next(), None);