서로 다른 에러에 대해 매칭
let greeting_file = match File::open("hello.txt") {
Ok(file) => file,
Err(error) => match error.kind() {
ErrorKind::NotFound => match File::create("hello.txt") {
Ok(fc) => fc,
Err(e) => panic!("Problem creating the file: {:?}", e),
},
other_error => {
panic!("Problem opening the file: {:?}", other_error);
}
},
};
- io::ErrorKind는 io 연산중 발생할 수 있는 다양한 에러의 열거형
- 파일을 여는데 못찾는 에러인 경우 파일을 생성하도록 처리
- 생성마저 실패하면 panic!
- 파일을 여는데 그 외의 에러인 경우 panic!