@@ -21761,7 +21761,7 @@ <h4 id="run関数からエラーを返す"><a class="header" href="#run関数か
21761
21761
21762
21762
// --snip--
21763
21763
21764
- fn run(config: Config) -> Result<(), Box<Error>> {
21764
+ fn run(config: Config) -> Result<(), Box<dyn Error>> {
21765
21765
let mut f = File::open(config.filename)?;
21766
21766
21767
21767
let mut contents = String::new();
@@ -21779,26 +21779,26 @@ <h4 id="run関数からエラーを返す"><a class="header" href="#run関数か
21779
21779
<p><span class="caption">リスト12-12: <code>run</code>関数を変更して<code>Result</code>を返す</span></p>
21780
21780
<!--
21781
21781
We’ve made three significant changes here. First, we changed the return type of
21782
- the `run` function to `Result<(), Box<Error>>`. This function previously
21782
+ the `run` function to `Result<(), Box<dyn Error>>`. This function previously
21783
21783
returned the unit type, `()`, and we keep that as the value returned in the
21784
21784
`Ok` case.
21785
21785
-->
21786
- <p>ここでは、3つの大きな変更を行いました。まず、<code>run</code>関数の戻り値を<code>Result<(), Box<Error>></code>に変えました。
21786
+ <p>ここでは、3つの大きな変更を行いました。まず、<code>run</code>関数の戻り値を<code>Result<(), Box<dyn Error>></code>に変えました。
21787
21787
この関数は、以前はユニット型、<code>()</code>を返していて、それを<code>Ok</code>の場合に返される値として残しました。</p>
21788
21788
<!--
21789
- For the error type, we used the *trait object* `Box<Error>` (and we’ve brought
21790
- `std::error::Error` into scope with a `use` statement at the top). We’ll cover
21791
- trait objects in Chapter 17. For now, just know that `Box<Error>` means the
21792
- function will return a type that implements the `Error` trait, but we don’t
21793
- have to specify what particular type the return value will be. This gives us
21794
- flexibility to return error values that may be of different types in different
21795
- error cases.
21789
+ For the error type, we used the *trait object* `Box<dyn Error>` (and we’ve
21790
+ brought `std::error::Error` into scope with a `use` statement at the top).
21791
+ We’ll cover trait objects in Chapter 17. For now, just know that `Box<dyn
21792
+ Error>` means the function will return a type that implements the `Error`
21793
+ trait, but we don’t have to specify what particular type the return value will
21794
+ be. This gives us flexibility to return error values that may be of different
21795
+ types in different error cases. The `dyn` keyword is short for “dynamic.”
21796
21796
-->
21797
- <p>エラー型については、<em>トレイトオブジェクト</em>の<code>Box<Error></code>を使用しました(同時に冒頭で<code>use</code>文により、
21797
+ <p>エラー型については、<em>トレイトオブジェクト</em>の<code>Box<dyn Error></code>を使用しました(同時に冒頭で<code>use</code>文により、
21798
21798
<code>std::error::Error</code>をスコープに導入しています)。トレイトオブジェクトについては、第17章で講義します。
21799
- とりあえず、<code>Box<Error></code>は、関数が<code>Error</code>トレイトを実装する型を返すことを意味しますが、
21799
+ とりあえず、<code>Box<dyn Error></code>は、関数が<code>Error</code>トレイトを実装する型を返すことを意味しますが、
21800
21800
戻り値の型を具体的に指定しなくても良いことを知っておいてください。これにより、
21801
- エラーケースによって異なる型のエラー値を返す柔軟性を得ます。</p>
21801
+ エラーケースによって異なる型のエラー値を返す柔軟性を得ます。<code>dyn</code> キーワードは、"dynamic"の略です。< /p>
21802
21802
<!--
21803
21803
Second, we’ve removed the calls to `expect` in favor of the `?` operator, as we
21804
21804
talked about in Chapter 9. Rather than `panic!` on an error, the `?` operator
@@ -21941,7 +21941,7 @@ <h3 id="コードをライブラリクレートに分割する"><a class="header
21941
21941
}
21942
21942
}
21943
21943
21944
- pub fn run(config: Config) -> Result<(), Box<Error>> {
21944
+ pub fn run(config: Config) -> Result<(), Box<dyn Error>> {
21945
21945
// --snip--
21946
21946
}
21947
21947
</code></pre>
0 commit comments