mirror of
https://github.com/KaiserY/trpl-zh-cn
synced 2025-02-23 12:52:20 +08:00
Merge pull request #331 from fooofei/master
ch09-02/map_err 使用错误,对齐英文原文
This commit is contained in:
commit
46317f699d
@ -135,19 +135,19 @@ use std::fs::File;
|
|||||||
use std::io::ErrorKind;
|
use std::io::ErrorKind;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let f = File::open("hello.txt").map_err(|error| {
|
let f = File::open("hello.txt").unwrap_or_else(|error| {
|
||||||
if error.kind() == ErrorKind::NotFound {
|
if error.kind() == ErrorKind::NotFound {
|
||||||
File::create("hello.txt").unwrap_or_else(|error| {
|
File::create("hello.txt").unwrap_or_else(|error| {
|
||||||
panic!("Tried to create file but there was a problem: {:?}", error);
|
panic!("Problem creating the file: {:?}", error);
|
||||||
})
|
})
|
||||||
} else {
|
} else {
|
||||||
panic!("There was a problem opening the file: {:?}", error);
|
panic!("Problem opening the file: {:?}", error);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
在阅读完第十三章后再回到这个例子,并查看标准库文档 `map_err` 和 `unwrap_or_else` 方法都做了什么操作。还有很多这类方法可以消除大量处理错误时嵌套的 `match` 表达式。
|
在阅读完第十三章后再回到这个例子,并查看标准库文档 `unwrap_or_else` 方法都做了什么操作。在处理错误时,还有很多这类技巧可以消除大量嵌套的 `match` 表达式。
|
||||||
|
|
||||||
### 失败时 panic 的简写:`unwrap` 和 `expect`
|
### 失败时 panic 的简写:`unwrap` 和 `expect`
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user