如何匹配特定的io :: Error类型? [英] How can I match on a specific io::Error type?
本文介绍了如何匹配特定的io :: Error类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图一次读入一个文件,直到最后2个字节为止,我想捕获EOF错误:
I am trying to read in a file until the end 2 bytes at a time and I want to catch the EOF error:
use byteorder::{BigEndian, ReadBytesExt}; // 1.3.4
use std::fs::File;
fn main() {
let filename = "/etc/hosts";
let mut file = File::open(filename).expect("Cannot open file");
loop {
let binary = match file.read_u16::<BigEndian>() {
Ok(binary) => binary,
Err(e) => panic!("Can't read from file: {}, err {}", filename, e),
// Can I catch the EOF error here?
};
println!("{:?}", binary);
}
}
推荐答案
此在Rust版本1.17.0中工作(并且可能会回到Rust 1.0):
This works in Rust version 1.17.0 (and probably back to Rust 1.0):
let binary = match file.read_u16::<BigEndian>() {
Err(ref e) if e.kind() == std::io::ErrorKind::UnexpectedEof => break,
Err(e) => panic!("Can't read from file: {}, err {}", filename, e),
Ok(binary) => binary,
};
这篇关于如何匹配特定的io :: Error类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文