@@ -34,12 +34,6 @@ Rustでは戻り値を使います。
34
34
というのは、直和型(sum type) とコンビネータから始めることで、Restにおけるエラーハンドリングを徐々に改善していくための動機を与えるからです。
35
35
このような構成ですので、もしすでに他の表現力豊かな型システムの経験があるプログラマでしたら、あちこち拾い読みしたくなるかもしれません。
36
36
37
- <!-- 用語集候補:combinator -->
38
-
39
- <!-- 翻訳メモ:sum type -->
40
- <!-- https://www.quora.com/What-is-a-sum-type -->
41
- <!-- http://d-poppo.nazo.cc/blog/2015/01/union-types/ -->
42
-
43
37
<!-- * [The Basics](#the-basics) -->
44
38
<!-- * [Unwrapping explained](#unwrapping-explained) -->
45
39
<!-- * [The `Option` type](#the-option-type) -->
@@ -70,16 +64,16 @@ Rustでは戻り値を使います。
70
64
<!-- * [Adding functionality](#adding-functionality) -->
71
65
<!-- [The short story](#the-short-story) -->
72
66
73
- * [ 基礎] ( #the-basics )
74
- * [ アンラップ(unwrap) とは] ( #unwrapping-explained )
75
- * [ ` Option ` 型] ( #the- option-type )
76
- * [ ` Option<T> ` 値で構成する ] ( #composing- optiont-values )
77
- * [ ` Result ` 型] ( #the- result-type )
78
- * [ 整数をパースする] ( #parsing-integers )
79
- * [ ` Result ` 型エイリアスを用いたイディオム] ( #the- result-type-alias-idiom )
80
- * [ 小休止:アンラップは悪ではない] ( #a-brief-interlude-unwrapping-isnt-evil )
67
+ * [ 基礎] ( #基礎 )
68
+ * [ アンラップ(unwrap) とは] ( #アンラップunwrap-とは )
69
+ * [ ` Option ` 型] ( #option-型 )
70
+ * [ ` Option<T> ` 値を合成する ] ( #optiont-値を合成する )
71
+ * [ ` Result ` 型] ( #result-型 )
72
+ * [ 整数をパースする] ( #整数をパースする )
73
+ * [ ` Result ` 型エイリアスを用いたイディオム] ( #result-型エイリアスを用いたイディオム )
74
+ * [ 小休止:アンラップは悪ではない] ( #小休止アンラップは悪ではない )
81
75
* [ 複数のエラー型を扱う] ( #working-with-multiple-error-types )
82
- * [ ` Option ` と ` Result ` で構成する ] ( #composing-option-and-result )
76
+ * [ ` Option ` と ` Result ` を合成する ] ( #composing-option-and-result )
83
77
* [ コンビネータの限界] ( #the-limits-of-combinators )
84
78
* [ 早期のリターン] ( #early-returns )
85
79
* [ ` try! ` マクロ] ( #the-try-macro )
@@ -88,7 +82,7 @@ Rustでは戻り値を使います。
88
82
* [ ` Error ` トレイト] ( #the-error-trait )
89
83
* [ ` From ` トレイト] ( #the-from-trait )
90
84
* [ 本当の ` try! ` マクロ] ( #the-real-try-macro )
91
- * [ 独自のエラー型で構成する ] ( #composing-custom-error-types )
85
+ * [ 独自のエラー型を合成する ] ( #composing-custom-error-types )
92
86
* [ ライブラリ作者たちへのアドバイス] ( #advice-for-library-writers )
93
87
* [ ケーススタディ:人口データを読み込むプログラム] ( #case-study-a-program-to-read-population-data )
94
88
* [ 最初のセットアップ] ( #initial-setup )
@@ -101,7 +95,6 @@ Rustでは戻り値を使います。
101
95
* [ 簡単な説明(まとめ)] ( #the-short-story )
102
96
103
97
<!-- # The Basics -->
104
- <span id =" the-basics " ></span >
105
98
# 基礎
106
99
107
100
<!-- You can think of error handling as using *case analysis* to determine whether -->
@@ -182,7 +175,6 @@ fn main() {
182
175
でも彼は、途中にある、あらゆるものを蹴散らしてしまいます。
183
176
184
177
<!-- ## Unwrapping explained -->
185
- <span id =" unwrapping-explained " ></span >
186
178
## アンラップ(unwrap) とは
187
179
188
180
<!-- In the previous example, we claimed -->
@@ -206,7 +198,6 @@ Rustでなにかを「アンラップする」時、こう言っているのと
206
198
どちらの型にも ` unwrap ` という名前のメソッドが定義されています。
207
199
208
200
<!-- ### The `Option` type -->
209
- <span id =" the-option-type " ></span >
210
201
### ` Option ` 型
211
202
212
203
<!-- The `Option` type is [defined in the standard library][5]: -->
@@ -332,8 +323,7 @@ impl<T> Option<T> {
332
323
しかし残念なことに、そこにある ` panic! ` が意味するものは、` unwrap ` が合成可能ではない、つまり、陶器店の中の雄牛だということです。
333
324
334
325
<!-- - ### Composing `Option<T>` values -->
335
- <span id =" composing-optiont-values " ></span >
336
- ### ` Option<T> ` 値で構成する
326
+ ### ` Option<T> ` 値を合成する
337
327
338
328
<!-- In an [example from before](#code-option-ex-string-find), -->
339
329
<!-- we saw how to use `find` to discover the extension in a file name. Of course, -->
@@ -482,7 +472,7 @@ fn main() {
482
472
<!-- need the file name which is typically extracted from a file *path*. While most -->
483
473
<!-- file paths have a file name, not *all* of them do. For example, `.`, `..` or -->
484
474
<!-- `/`. -->
485
- もうひとつ注目すべきコンビネータがあります 。それは ` and_then ` です。これを使うと * 不在の可能性* を考慮しながら、別々の処理を簡単に組み合わせることができます。
475
+ もうひとつ特筆すべきコンビネータがあります 。それは ` and_then ` です。これを使うと * 不在の可能性* を考慮しながら、別々の処理を簡単に組み合わせることができます。
486
476
例えば、この節のほとんどのコードは、与えられたファイル名について拡張子を見つけだします。
487
477
そのためには、まずファイル * パス* から取り出したファイル名が必要です。
488
478
大抵のパスにはファイル名がありますが、 * 全て* がというわけではありません。
@@ -567,7 +557,6 @@ fn file_path_ext(file_path: &str) -> Option<&str> {
567
557
` unwrap ` のようなメソッドは、 ` Option<T> ` が ` None ` の時にパニックを起こすので、このような選択の機会を与えません。
568
558
569
559
<!-- ## The `Result` type -->
570
- <span id =" the-result-type " ></span >
571
560
## ` Result ` 型
572
561
573
562
<!-- The `Result` type is also -->
@@ -605,7 +594,7 @@ type Option<T> = Result<T, ()>;
605
594
<!-- the type and value level terms have the same notation!) -->
606
595
これは ` Result ` の2番目の型パラメータを ` () ` (「ユニット」または「空タプル」と発音します)に固定したものです。
607
596
` () ` 型のただ一つの値は ` () ` です。
608
- (そうなんです。型レベルと値レベルの用語が 、全く同じ表記法を持ちます!)
597
+ (そうなんです。型レベルと値レベルの項が 、全く同じ表記法を持ちます!)
609
598
610
599
<!-- The `Result` type is a way of representing one of two possible outcomes in a -->
611
600
<!-- computation. By convention, one outcome is meant to be expected or “`Ok`” while -->
@@ -618,6 +607,7 @@ type Option<T> = Result<T, ()>;
618
607
<!-- defined][7] -->
619
608
<!-- in the standard library. Let's define it: -->
620
609
` Option ` と全く同じように、` Result ` 型も標準ライブラリで [ ` unwrap ` メソッドが定義されています] [ 7 ] 。
610
+ 定義してみましょう:
621
611
622
612
``` rust
623
613
# enum Result <T , E > { Ok (T ), Err (E ) }
@@ -654,7 +644,6 @@ impl<T, E: ::std::fmt::Debug> Result<T, E> {
654
644
では、例を見ていきましょう。
655
645
656
646
<!-- ### Parsing integers -->
657
- <span id =" parsing-integers " ></span >
658
647
### 整数をパースする
659
648
660
649
<!-- The Rust standard library makes converting strings to integers dead simple. -->
@@ -737,7 +726,7 @@ impl str {
737
726
この関数を同じように一般化することもできますが(そして、そうするべきでしょうが)、今は明快さを優先しましょう。
738
727
` i32 ` だけを扱うことにしますので、それの [ ` FromStr ` の実装がどうなっているか探しましょう] ( ../std/primitive.i32.html ) 。
739
728
(ブラウザで ` CTRL-F ` を押して「FromStr」を探します。)
740
- そして [ 関連型( associated type) ] [ 10 ] から ` Err ` を見つけます。
729
+ そして [ 関連型( associated type) ] [ 10 ] から ` Err ` を見つけます。
741
730
こうすれば、具体的なエラー型が見つかります。
742
731
この場合、それは [ ` std::num::ParseIntError ` ] ( ../std/num/struct.ParseIntError.html ) です。
743
732
これでようやく関数を書き直せます:
@@ -800,7 +789,6 @@ fn main() {
800
789
さらに ` Result ` は2つ目の型パラメータを取りますので、エラー型だけに影響を与える [ ` map_err ` ] ( ../std/result/enum.Result.html#method.map_err ) (` map ` に相当)と [ ` or_else ` ] ( ../std/result/enum.Result.html#method.or_else ) (` and_then ` に相当)もあります。
801
790
802
791
<!-- ### The `Result` type alias idiom -->
803
- <span id =" the-result-type-alias-idiom " ></span >
804
792
### ` Result ` 型エイリアスを用いたイディオム
805
793
806
794
<!-- In the standard library, you may frequently see types like -->
@@ -846,7 +834,6 @@ fn double_number(number_str: &str) -> Result<i32> {
846
834
それらは通常 ` io::Result<T> ` のように書かれ、` std::result ` のプレーンな定義の代わりに ` io ` モジュールの型エイリアスを使っていることが、明確にわかるようになっています。
847
835
848
836
<!-- ## A brief interlude: unwrapping isn't evil -->
849
- <span id =" a-brief-interlude-unwrapping-isnt-evil " ></span >
850
837
## 小休止:アンラップは悪ではない
851
838
852
839
<!-- If you've been following along, you might have noticed that I've taken a pretty -->
@@ -877,7 +864,7 @@ fn double_number(number_str: &str) -> Result<i32> {
877
864
<!-- exposes a bug in your program. This can be explicit, like from an `assert!` -->
878
865
<!-- failing, or it could be because your index into an array was out of bounds. -->
879
866
* ** パニックがプログラムのバグの兆候となる時。**
880
- コードの中の不変的な条件が 、ある特定のケースの発生を未然に防ぐ時(例えば、空のスタックから取り出そうとしたなど)、パニックを起こしても差し支えありません。
867
+ コードの中の不変条件が 、ある特定のケースの発生を未然に防ぐ時(例えば、空のスタックから取り出そうとしたなど)、パニックを起こしても差し支えありません。
881
868
なぜなら、そうすることでプログラムに潜むバグが明るみに出るからです。
882
869
これは ` assert! ` の失敗のような明示的な要因によるものだったり、配列のインデックスが境界から外れたからだったりします。
883
870
0 commit comments