@@ -21761,7 +21761,7 @@ <h4 id="run関数からエラーを返す"><a class="header" href="#run関数か
2176121761
2176221762// --snip--
2176321763
21764- fn run(config: Config) -> Result<(), Box<Error>> {
21764+ fn run(config: Config) -> Result<(), Box<dyn Error>> {
2176521765 let mut f = File::open(config.filename)?;
2176621766
2176721767 let mut contents = String::new();
@@ -21779,26 +21779,26 @@ <h4 id="run関数からエラーを返す"><a class="header" href="#run関数か
2177921779<p><span class="caption">リスト12-12: <code>run</code>関数を変更して<code>Result</code>を返す</span></p>
2178021780<!--
2178121781We’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
2178321783returned the unit type, `()`, and we keep that as the value returned in the
2178421784`Ok` case.
2178521785-->
21786- <p>ここでは、3つの大きな変更を行いました。まず、<code>run</code>関数の戻り値を<code>Result<(), Box<Error>></code>に変えました。
21786+ <p>ここでは、3つの大きな変更を行いました。まず、<code>run</code>関数の戻り値を<code>Result<(), Box<dyn Error>></code>に変えました。
2178721787この関数は、以前はユニット型、<code>()</code>を返していて、それを<code>Ok</code>の場合に返される値として残しました。</p>
2178821788<!--
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.”
2179621796-->
21797- <p>エラー型については、<em>トレイトオブジェクト</em>の<code>Box<Error></code>を使用しました(同時に冒頭で<code>use</code>文により、
21797+ <p>エラー型については、<em>トレイトオブジェクト</em>の<code>Box<dyn Error></code>を使用しました(同時に冒頭で<code>use</code>文により、
2179821798<code>std::error::Error</code>をスコープに導入しています)。トレイトオブジェクトについては、第17章で講義します。
21799- とりあえず、<code>Box<Error></code>は、関数が<code>Error</code>トレイトを実装する型を返すことを意味しますが、
21799+ とりあえず、<code>Box<dyn Error></code>は、関数が<code>Error</code>トレイトを実装する型を返すことを意味しますが、
2180021800戻り値の型を具体的に指定しなくても良いことを知っておいてください。これにより、
21801- エラーケースによって異なる型のエラー値を返す柔軟性を得ます。</p>
21801+ エラーケースによって異なる型のエラー値を返す柔軟性を得ます。<code>dyn</code> キーワードは、"dynamic"の略です。< /p>
2180221802<!--
2180321803Second, we’ve removed the calls to `expect` in favor of the `?` operator, as we
2180421804talked about in Chapter 9. Rather than `panic!` on an error, the `?` operator
@@ -21941,7 +21941,7 @@ <h3 id="コードをライブラリクレートに分割する"><a class="header
2194121941 }
2194221942}
2194321943
21944- pub fn run(config: Config) -> Result<(), Box<Error>> {
21944+ pub fn run(config: Config) -> Result<(), Box<dyn Error>> {
2194521945 // --snip--
2194621946}
2194721947</code></pre>
0 commit comments