更新时间:2023-11-14 11:39:40
您不必重新实现 BufReader
本身,它为您的用例提供了所需的方法 read_until
:
You do not have to re-implement BufReader
itself, it provides exactly the method you need for your usecase read_until
:
fn read_until(&mut self, byte: u8, buf: &mut Vec<u8>) -> Result<usize>
您提供自己的Vec
,文件的内容将被附加,直到遇到byte
(0x0A 为LF).
You supply your own Vec<u8>
and the content of the file will be appended until byte
is encountered (0x0A being LF).
有几个潜在的问题:
buf
由您决定.buf
between subsequent calls.一个简单的while let Ok(_) = reader.read_until(0x0A as u8, buffer)
应该让你很容易阅读你的文件.
A simple while let Ok(_) = reader.read_until(0x0A as u8, buffer)
should let you read your file easily enough.
你可以考虑实现一个 std::io::Lines
等价物,它从编码转换为 UTF-8 以提供一个很好的 API,尽管它会降低性能.
You may consider implement a std::io::Lines
equivalent which converts from the encoding to UTF-8 to provide a nice API, though it will have a performance cost.