1 unstable release
0.1.0 | Mar 15, 2023 |
---|
#1725 in Filesystem
10KB
66 lines
QRead
The "Q" is for "Quick".
QRead is convenience library for common read operations. In Rust, the standard library Read
and BufRead
traits expose many methods that require you to pass in a mutable reference to a buffer (either a Vec<u8>
or String
) that it will store the data read into. Often, you will find yourself repeating the pattern of creating a Vec
or String
just to pass it into these methods. This can become inconvenient if you need to repeat this pattern many times.
QRead
addresses this by adding two new methods (read_all_to_bytes
and read_all_to_string
) to all objects implementing the Read
trait and two other new methods (read_until_to_bytes
and read_line_to_string
) to all objects implementing the BufRead
trait. These methods internally just call the most common methods on Read
or BufRead
after creating the Vec
or String
for you:
use std::fs::File;
use qread::QRead;
// This assumes that we have a file `demo.txt` in the current directory
let mut f2 = File::open("demo.txt").unwrap();
let text = f2.read_all_to_string().unwrap();
See the docs for more examples.