#lines #bytes


Read input lines as byte slices for high efficiency

9 stable releases

2.4.0 Apr 4, 2022
2.3.0 Apr 4, 2022
2.2.2 Jan 10, 2019
1.0.1 Dec 14, 2018

#157 in Text processing

Download history 650/week @ 2022-12-06 548/week @ 2022-12-13 456/week @ 2022-12-20 489/week @ 2022-12-27 555/week @ 2023-01-03 757/week @ 2023-01-10 740/week @ 2023-01-17 610/week @ 2023-01-24 715/week @ 2023-01-31 525/week @ 2023-02-07 582/week @ 2023-02-14 926/week @ 2023-02-21 1344/week @ 2023-02-28 1063/week @ 2023-03-07 726/week @ 2023-03-14 532/week @ 2023-03-21

3,827 downloads per month
Used in 13 crates (10 directly)

MIT license

225 lines


Crates.io Build Status

This library provides an easy way to read in input lines as byte slices for high efficiency. It's basically lines from the standard library, but it reads each line as a byte slice (&[u8]). This performs significantly faster than lines() in the case you don't particularly care about unicode, and basically as fast as writing the loops out by hand. Although the code itself is somewhat trivial, I've had to roll this in at least 4 tools I've written recently and so I figured it was time to have a convenience crate for it.


This tool will be available via Crates.io, so you can add it as a dependency in your Cargo.toml:

bytelines = "2.4"


It's quite simple; in the place you would typically call lines on a BufRead implementor, you can now use bytelines to retrieve a structure used to walk over lines as &[u8] (and thus avoid allocations). There are two ways to use the API, and both are shown below:

// our input file we're going to walk over lines of, and our reader
let file = File::open("./my-input.txt").expect("able to open file");
let reader = BufReader::new(file);
let mut lines = ByteLines::new(reader);

// Option 1: Walk using a `while` loop.
// This is the most performant option, as it avoids an allocation by
// simply referencing bytes inside the reading structure. This means
// that there's no copying at all, until the developer chooses to.
while let Some(line) = lines.next() {
    // do something with the line

// Option 2: Use the `Iterator` trait.
// This is more idiomatic, but requires allocating each line into
// an owned `Vec` to avoid potential memory safety issues. Although
// there is an allocation here, the overhead should be negligible
// except in cases where performance is paramount.
for line in lines.into_iter() {
    // do something with the line

As of v2.3 this crate includes fairly minimal support for Tokio, namely the AsyncBufRead trait. This looks fairly similar to the base APIs, and can be used in much the same way.

// configure our inputs again, using `AsyncByteLines`.
let file = File::open("./my-input.txt").await?;
let reader = BufReader::new(file);
let mut lines = AsyncByteLines::new(reader);

// walk through all lines using a `while` loop
while let Some(line) = lines.next().await? {
    // do something with the line

// walk through all lines using `Stream` APIs
lines.into_stream().for_each(|line| {


The main difference is that the Tokio implementations yield Result<Option<&[u8]>, _> instead of Option<Result<&[u8], _>> for consistency with the exiting Tokio APIs.


~101K SLoC