Skip to content

Commit 6e758d2

Browse files
authored
Merge pull request #214 from matsuokashuhei/fix/ch12-03-improving-error-handling-and-modularity
Add `dyn` keyword to `Box<Error>` type in Chapter 12
2 parents 5b78ae2 + 4b36277 commit 6e758d2

File tree

1 file changed

+14
-14
lines changed

1 file changed

+14
-14
lines changed

src/ch12-03-improving-error-handling-and-modularity.md

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -910,7 +910,7 @@ use std::error::Error;
910910
911911
// --snip--
912912
913-
fn run(config: Config) -> Result<(), Box<Error>> {
913+
fn run(config: Config) -> Result<(), Box<dyn Error>> {
914914
let mut f = File::open(config.filename)?;
915915
916916
let mut contents = String::new();
@@ -931,29 +931,29 @@ fn run(config: Config) -> Result<(), Box<Error>> {
931931

932932
<!--
933933
We’ve made three significant changes here. First, we changed the return type of
934-
the `run` function to `Result<(), Box<Error>>`. This function previously
934+
the `run` function to `Result<(), Box<dyn Error>>`. This function previously
935935
returned the unit type, `()`, and we keep that as the value returned in the
936936
`Ok` case.
937937
-->
938938

939-
ここでは、3つの大きな変更を行いました。まず、`run`関数の戻り値を`Result<(), Box<Error>>`に変えました。
939+
ここでは、3つの大きな変更を行いました。まず、`run`関数の戻り値を`Result<(), Box<dyn Error>>`に変えました。
940940
この関数は、以前はユニット型、`()`を返していて、それを`Ok`の場合に返される値として残しました。
941941

942942
<!--
943-
For the error type, we used the *trait object* `Box<Error>` (and we’ve brought
944-
`std::error::Error` into scope with a `use` statement at the top). We’ll cover
945-
trait objects in Chapter 17. For now, just know that `Box<Error>` means the
946-
function will return a type that implements the `Error` trait, but we don’t
947-
have to specify what particular type the return value will be. This gives us
948-
flexibility to return error values that may be of different types in different
949-
error cases.
943+
For the error type, we used the *trait object* `Box<dyn Error>` (and we’ve
944+
brought `std::error::Error` into scope with a `use` statement at the top).
945+
We’ll cover trait objects in Chapter 17. For now, just know that `Box<dyn
946+
Error>` means the function will return a type that implements the `Error`
947+
trait, but we don’t have to specify what particular type the return value will
948+
be. This gives us flexibility to return error values that may be of different
949+
types in different error cases. The `dyn` keyword is short for “dynamic.”
950950
-->
951951

952-
エラー型については、*トレイトオブジェクト*`Box<Error>`を使用しました(同時に冒頭で`use`文により、
952+
エラー型については、*トレイトオブジェクト*`Box<dyn Error>`を使用しました(同時に冒頭で`use`文により、
953953
`std::error::Error`をスコープに導入しています)。トレイトオブジェクトについては、第17章で講義します。
954-
とりあえず、`Box<Error>`は、関数が`Error`トレイトを実装する型を返すことを意味しますが、
954+
とりあえず、`Box<dyn Error>`は、関数が`Error`トレイトを実装する型を返すことを意味しますが、
955955
戻り値の型を具体的に指定しなくても良いことを知っておいてください。これにより、
956-
エラーケースによって異なる型のエラー値を返す柔軟性を得ます。
956+
エラーケースによって異なる型のエラー値を返す柔軟性を得ます。`dyn` キーワードは、"dynamic"の略です。
957957

958958
<!--
959959
Second, we’ve removed the calls to `expect` in favor of the `?` operator, as we
@@ -1131,7 +1131,7 @@ impl Config {
11311131
}
11321132
}
11331133
1134-
pub fn run(config: Config) -> Result<(), Box<Error>> {
1134+
pub fn run(config: Config) -> Result<(), Box<dyn Error>> {
11351135
// --snip--
11361136
}
11371137
```

0 commit comments

Comments
 (0)