Skip to main content

stream_lines

Function stream_lines 

Source
pub fn stream_lines<F>(path: &Path, line_fn: F) -> Result<usize>
where F: FnMut(usize, &str) -> Result<()>,
Expand description

Processes a file by reading line-by-line with constant memory.

Calls line_fn for each line. The line buffer is reused across iterations — memory does not grow with file length.

§Examples

use ssg::stream::stream_lines;
use tempfile::tempdir;
use std::fs;

let dir = tempdir().unwrap();
let p = dir.path().join("f.txt");
fs::write(&p, "a\nb\nc").unwrap();
let mut seen = Vec::new();
let n = stream_lines(&p, |_, line| { seen.push(line.to_string()); Ok(()) }).unwrap();
assert_eq!(n, 3);
assert_eq!(seen[0], "a");

§Errors

Returns an error if the file cannot be read.