rev_lines icon indicating copy to clipboard operation
rev_lines copied to clipboard

Implementation of FromIterator<String, RevLinesError>

Open xTriixrx opened this issue 9 months ago • 0 comments

I am using this library to get the first N amount of lines from a file in reverse... my current implementation is this:

fn lines_from_file(file: &File, limit: usize) -> Vec<String> {
    let mut vec = vec![];
    let rev_lines = RevLines::new(file);

    for (i, line) in rev_lines.enumerate() {
        match line {
            Ok(line) => vec.push(line),
            Err(e) => panic!("RevLinesError in lines_from_file: {}", e),
        }
        if i == limit {
            break;
        }
    }
    return vec;
}

Ideally, if the FromIterator<String, RevLinesError> interface was implemented I could do roughly something like this:

fn lines_from_file(file: &File, limit: usize) -> Vec<String> {
    let rev_lines = RevLines::new(file);
    rev_lines.take(limit).collect()
}

xTriixrx avatar Mar 17 '25 03:03 xTriixrx