File tree Expand file tree Collapse file tree 2 files changed +18
-1
lines changed Expand file tree Collapse file tree 2 files changed +18
-1
lines changed Original file line number Diff line number Diff line change 222
222
- [ テストケース: 空トレイト] ( generics/bounds/testcase_empty.md )
223
223
- [ 複数のジェネリック境界] ( generics/multi_bounds.md )
224
224
- [ Where句] ( generics/where.md )
225
- - [ New Type Idiom ] ( generics/new_types.md )
225
+ - [ ニュータイプイディオム ] ( generics/new_types.md )
226
226
- [ 関連型] ( generics/assoc_items.md )
227
227
- [ 関連型が必要になる状況] ( generics/assoc_items/the_problem.md )
228
228
- [ 関連型] ( generics/assoc_items/types.md )
Original file line number Diff line number Diff line change
1
+ <!--
1
2
# New Type Idiom
3
+ -->
4
+ # ニュータイプイディオム
2
5
6
+ <!--
3
7
The `newtype` idiom gives compile time guarantees that the right type of value is supplied
4
8
to a program.
9
+ -->
10
+ 既に定義済みの型であっても` struct ` を使うことであたかも別の型` newtype ` であるかのように定義することが可能です。なお、それらが正しい型としてプログラムに供給されているか否かは、コンパイル時に保証されます。
5
11
12
+ <!--
6
13
For example, an age verification function that checks age in years, *must* be given
7
14
a value of type `Years`.
15
+ -->
16
+ 例えば、年齢を年単位で確認する` old_enough ` には「Years」という型の値を * 与えなければならない* ようにすることが可能です。
8
17
9
18
``` rust, editable
10
19
struct Years(i64);
@@ -20,6 +29,7 @@ impl Years {
20
29
21
30
impl Days {
22
31
/// truncates partial years
32
+ /// 1年に満たない日付は切り捨て
23
33
pub fn to_years(&self) -> Years {
24
34
Years(self.0 / 365)
25
35
}
@@ -38,9 +48,16 @@ fn main() {
38
48
}
39
49
```
40
50
51
+ <!--
41
52
Uncomment the last print statement to observe that the type supplied must be `Years`.
53
+ -->
54
+ 最後の print文 のコメントを外して、与えられた型が ` Years ` でなければならないことを確認してください。
42
55
56
+ <!--
43
57
To obtain the `newtype`'s value as the base type, you may use the tuple or destructuring syntax like so:
58
+ -->
59
+ ` newtype ` の元に使われている型のデータを取得するには、以下のようにタプルや分配構文を用いることで取得できます。
60
+
44
61
``` rust, editable
45
62
struct Years(i64);
46
63
You can’t perform that action at this time.
0 commit comments