本文介绍了在读取文件时检测EndOfFile IoResult的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试读取Rust中的文件。我不明白的是:当 BufferedReader
在EOF时,它实际上给出了一个 Err(IoError {kind:EndOfFile})
我不知道如何匹配它。
I'm trying to read from a file in Rust. The thing I don't understand is: when the BufferedReader
is at the EOF, it actually gives an Err(IoError {kind: EndOfFile})
and I don't know how to match on it.
loop {
match file.read_line() {
Ok(line) => {
// Do stuff
},
// Err(EndOfFile) => is want I want here so I can call "break"
Err(_) => {
panic!("Unexpected error reading file.");
}
}
}
如何明确匹配 EndOfFile
enum variant?
How can I explicitly match the EndOfFile
enum variant?
推荐答案
您可以使用以下模式匹配它而无需额外的嵌套匹配:
You can match it without additional nested match using following pattern:
loop {
match file.read_line() {
Ok(line) => {
// Do stuff
},
Err(IoError { kind: EndOfFile, .. }) =>
break,
Err(_) => {
panic!("Unexpected error reading file.");
}
}
}
这篇关于在读取文件时检测EndOfFile IoResult的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!