@@ -910,7 +910,7 @@ use std::error::Error;
910
910
911
911
// --snip--
912
912
913
- fn run(config: Config) -> Result<(), Box<Error>> {
913
+ fn run(config: Config) -> Result<(), Box<dyn Error>> {
914
914
let mut f = File::open(config.filename)?;
915
915
916
916
let mut contents = String::new();
@@ -931,29 +931,29 @@ fn run(config: Config) -> Result<(), Box<Error>> {
931
931
932
932
<!--
933
933
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
935
935
returned the unit type, `()`, and we keep that as the value returned in the
936
936
`Ok` case.
937
937
-->
938
938
939
- ここでは、3つの大きな変更を行いました。まず、` run ` 関数の戻り値を` Result<(), Box<Error>> ` に変えました。
939
+ ここでは、3つの大きな変更を行いました。まず、` run ` 関数の戻り値を` Result<(), Box<dyn Error>> ` に変えました。
940
940
この関数は、以前はユニット型、` () ` を返していて、それを` Ok ` の場合に返される値として残しました。
941
941
942
942
<!--
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.”
950
950
-->
951
951
952
- エラー型については、* トレイトオブジェクト* の` Box<Error> ` を使用しました(同時に冒頭で` use ` 文により、
952
+ エラー型については、* トレイトオブジェクト* の` Box<dyn Error> ` を使用しました(同時に冒頭で` use ` 文により、
953
953
` std::error::Error ` をスコープに導入しています)。トレイトオブジェクトについては、第17章で講義します。
954
- とりあえず、` Box<Error> ` は、関数が` Error ` トレイトを実装する型を返すことを意味しますが、
954
+ とりあえず、` Box<dyn Error> ` は、関数が` Error ` トレイトを実装する型を返すことを意味しますが、
955
955
戻り値の型を具体的に指定しなくても良いことを知っておいてください。これにより、
956
- エラーケースによって異なる型のエラー値を返す柔軟性を得ます。
956
+ エラーケースによって異なる型のエラー値を返す柔軟性を得ます。` dyn ` キーワードは、"dynamic"の略です。
957
957
958
958
<!--
959
959
Second, we’ve removed the calls to `expect` in favor of the `?` operator, as we
@@ -1131,7 +1131,7 @@ impl Config {
1131
1131
}
1132
1132
}
1133
1133
1134
- pub fn run(config: Config) -> Result<(), Box<Error>> {
1134
+ pub fn run(config: Config) -> Result<(), Box<dyn Error>> {
1135
1135
// --snip--
1136
1136
}
1137
1137
```
0 commit comments