【问题标题】:Rust read last x lines from fileRust 从文件中读取最后 x 行
【发布时间】:2022-11-02 05:17:00
【问题描述】:

目前,我在我的代码中使用这个函数:

fn lines_from_file(filename: impl AsRef<Path>) -> Vec<String> {
    let file = File::open(filename).expect("no such file");
    let buf = BufReader::new(file);
    buf.lines().map(|l| l.expect("Could not parse line")).collect()
}

我怎样才能安全地读取文件中的最后 x 行?

【问题讨论】:

标签: rust


【解决方案1】:

The tail crate 声称提供了一种通过the BackwardsReader struct 从文件中读取最终n 行的有效方法,并且看起来相当易于使用。我不能对它的效率发誓(看起来它执行越来越大的读取,在文件中越来越远,这相对于an optimized memory map-based solution 稍微次优),但它是一个简单的多合一包并且效率低下可能在 99% 的所有用例中都无关紧要。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多