Skip to content

Commit

Permalink
Update ch09-02-recoverable-errors-with-result.md
Browse files Browse the repository at this point in the history
  • Loading branch information
green961 authored Feb 9, 2024
1 parent 78c8463 commit 0650937
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion src/ch09-02-recoverable-errors-with-result.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ enum Result<T, E> {

`File::open` 返回的 `Err` 成员中的值类型 `io::Error`,它是一个标准库中提供的结构体。这个结构体有一个返回 `io::ErrorKind` 值的 `kind` 方法可供调用。`io::ErrorKind` 是一个标准库提供的枚举,它的成员对应 `io` 操作可能导致的不同错误类型。我们感兴趣的成员是 `ErrorKind::NotFound`,它代表尝试打开的文件并不存在。这样,`match` 就匹配完 `greeting_file_result` 了,不过对于 `error.kind()` 还有一个内层 `match`

我们希望在内层 `match` 中检查的条件是 `error.kind()` 的返回值是否为 `ErrorKind``NotFound` 成员。如果是,则尝试通过 `File::create` 创建文件。然而因为 `File::create` 也可能会失败,还需要增加一个内层 `match` 语句。当文件不能被打开,会打印出一个不同的错误信息。外层 `match` 的最后一个分支保持不变,这样对任何除了文件不存在的错误会使程序 panic。
我们希望在内层 `match` 中检查的条件是 `error.kind()` 的返回值是否为 `ErrorKind``NotFound` 成员。如果是,则尝试通过 `File::create` 创建文件。然而因为 `File::create` 也可能会失败,还需要增加一个内层 `match` 语句。当文件不能被创建,会打印出一个不同的错误信息。外层 `match` 的最后一个分支保持不变,这样对任何除了文件不存在的错误会使程序 panic。

> 不同于使用 `match``Result<T, E>`
>
Expand Down

0 comments on commit 0650937

Please sign in to comment.