【发布时间】: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 行?
【问题讨论】:
-
内存映射文件,连续向后扫描换行符
x + 1次,取出最后一个换行符之后的所有内容并将其转换为行? Same basic solution in any language。
标签: rust