Skip to content

Commit

Permalink
translate reenter_question_mark.md
Browse files Browse the repository at this point in the history
* error/multiple_error_types/reenter_question_mark.md
  • Loading branch information
a2not committed Aug 2, 2020
1 parent 906ac69 commit 2a2ea96
Showing 1 changed file with 22 additions and 3 deletions.
25 changes: 22 additions & 3 deletions src/error/multiple_error_types/reenter_question_mark.md
@@ -1,32 +1,45 @@
# Other uses of `?`
# `?`の他の活用法

<!--
Notice in the previous example that our immediate reaction to calling
`parse` is to `map` the error from a library error into a boxed
error:
-->
以前の例では`parse`の呼び出しに対するその場での対応として、エラーをライブラリのエラーからboxされたエラーへと`map`していました。

```rust,ignore
.and_then(|s| s.parse::<i32>()
.map_err(|e| e.into())
```

<!--
Since this is a simple and common operation, it would be convenient if it
could be elided. Alas, because `and_then` is not sufficiently flexible, it
cannot. However, we can instead use `?`.
-->
簡単でよくあるオペレーションのため、可能なら省略してしまえると便利だったでしょう。でも残念、`and_then`が十分にフレキシブルでないため、それはできません。ただその代わり、`?`なら使えます。

<!--
`?` was previously explained as either `unwrap` or `return Err(err)`.
This is only mostly true. It actually means `unwrap` or
`return Err(From::from(err))`. Since `From::from` is a conversion utility
between different types, this means that if you `?` where the error is
convertible to the return type, it will convert automatically.
-->
`?`の挙動は、`unwrap`または`return Err(err)`として説明されていました。これはほぼ正解で、本当は`unwrap`、もしくは`return Err(From::from(err))`という意味があります。`From::from`は異なる型の間での変換ユーティリティであることから、エラーがリターン型に変換可能な場合に`?`を使うことで、その変換を自動的に行ってくれます。

<!--
Here, we rewrite the previous example using `?`. As a result, the
`map_err` will go away when `From::from` is implemented for our error type:
-->
前の例を`?`を使ったものに書き換えてみましょう。その結果、`From::from`がエラー型に実装されている時`map_err`は消えてなくなります。

```rust,editable
use std::error;
use std::fmt;
// Change the alias to `Box<dyn error::Error>`.
// エイリアスを`Box<error::Error>`に変換する。
type Result<T> = std::result::Result<T, Box<dyn error::Error>>;
#[derive(Debug)]
Expand All @@ -45,12 +58,15 @@ impl error::Error for EmptyVec {
fn cause(&self) -> Option<&error::Error> {
// Generic error, underlying cause isn't tracked.
// 基本となるエラー、原因は記録されていない。
None
}
}
// The same structure as before but rather than chain all `Results`
// and `Options` along, we `?` to get the inner value out immediately.
// 前と同じ構造だが、`Results`と`Option`を繋げていくより、
// `?`で内部の値をその場で取得します。
fn double_first(vec: Vec<&str>) -> Result<i32> {
let first = vec.first().ok_or(EmptyVec)?;
let parsed = first.parse::<i32>()?;
Expand All @@ -75,14 +91,17 @@ fn main() {
}
```

<!--
This is actually fairly clean now. Compared with the original `panic`, it
is very similar to replacing the `unwrap` calls with `?` except that the
return types are `Result`. As a result, they must be destructured at the
top level.
-->
これでかなり綺麗になりました。元の`panic`と比べ、リターン型が`Result`であることを除けば、`unwrap`の呼び出しを`?`で置き換える点で非常に似ています。結果、最も上のレベルで分解されなければなりません。

### See also:
### 参考:

[`From::from`][from] and [`?`][q_mark]
[`From::from`][from] [`?`][q_mark]

[from]: https://doc.rust-lang.org/std/convert/trait.From.html
[q_mark]: https://doc.rust-lang.org/reference/expressions/operator-expr.html#the-question-mark-operator

0 comments on commit 2a2ea96

Please sign in to comment.