From dd8cb4d6ec28dd08115d4213c7763d068ec499cf Mon Sep 17 00:00:00 2001 From: fooofei Date: Thu, 29 Aug 2019 18:17:40 +0800 Subject: [PATCH] =?UTF-8?q?ch09-02/map=5Ferr=20=E4=BD=BF=E7=94=A8=E9=94=99?= =?UTF-8?q?=E8=AF=AF=EF=BC=8C=E5=AF=B9=E9=BD=90=E8=8B=B1=E6=96=87=E5=8E=9F?= =?UTF-8?q?=E6=96=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/ch09-02-recoverable-errors-with-result.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/ch09-02-recoverable-errors-with-result.md b/src/ch09-02-recoverable-errors-with-result.md index 049a668..2177c36 100644 --- a/src/ch09-02-recoverable-errors-with-result.md +++ b/src/ch09-02-recoverable-errors-with-result.md @@ -135,19 +135,19 @@ use std::fs::File; use std::io::ErrorKind; 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 { 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 { - 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`