std.reader
API summary (generated from the Beans source by npm run coverage): 1 type · 1 constructor · 1 instance method.
std.reader gives you a Reader that reads a File
one line at a time. It buffers behind the scenes so you are not making a syscall
per line. Read the source at
stdlib/std/reader/reader.b.
import std.readerReader
Section titled “Reader”You build one with new, handing it an open file:
pub class Readernew Reader(file: File)
pub fn read_line() -> Result<Option<string>>read_linereturnsok(some(line))for each line, with the trailing newline removed. At end of file it returnsok(none). If the read fails, you get an error.
The reader keeps its own offset and reads with pread, so it never moves the
underlying file’s cursor. You can read the same file another way at the same time
without the two interfering.
import std.ioimport std.reader
fn main() { let file: File = File.open("log.txt", "r").expect("open") let r: reader.Reader = new reader.Reader(file) for true { let line: Option<string> = r.read_line().expect("read") match line { some(text) => io.println(text), none => { break }, } } file.close()}See also
Section titled “See also”- Files and mapping, the
Filetype you open and pass in. - std.fs, read a whole file at once when you do not need it line by line.