Skip to content

Commit 79c3ec5

Browse files
authored
Merge pull request #112 from KANIYONIKA/Translate-generics/new_types.md
Translate generics/new types.md
2 parents 6d45dae + 645da47 commit 79c3ec5

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

src/SUMMARY.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@
222222
- [テストケース: 空トレイト](generics/bounds/testcase_empty.md)
223223
- [複数のジェネリック境界](generics/multi_bounds.md)
224224
- [Where句](generics/where.md)
225-
- [New Type Idiom](generics/new_types.md)
225+
- [ニュータイプイディオム](generics/new_types.md)
226226
- [関連型](generics/assoc_items.md)
227227
- [関連型が必要になる状況](generics/assoc_items/the_problem.md)
228228
- [関連型](generics/assoc_items/types.md)

src/generics/new_types.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,19 @@
1+
<!--
12
# New Type Idiom
3+
-->
4+
# ニュータイプイディオム
25

6+
<!--
37
The `newtype` idiom gives compile time guarantees that the right type of value is supplied
48
to a program.
9+
-->
10+
既に定義済みの型であっても`struct`を使うことであたかも別の型`newtype`であるかのように定義することが可能です。なお、それらが正しい型としてプログラムに供給されているか否かは、コンパイル時に保証されます。
511

12+
<!--
613
For example, an age verification function that checks age in years, *must* be given
714
a value of type `Years`.
15+
-->
16+
例えば、年齢を年単位で確認する`old_enough`には「Years」という型の値を *与えなければならない* ようにすることが可能です。
817

918
```rust, editable
1019
struct Years(i64);
@@ -20,6 +29,7 @@ impl Years {
2029
2130
impl Days {
2231
/// truncates partial years
32+
/// 1年に満たない日付は切り捨て
2333
pub fn to_years(&self) -> Years {
2434
Years(self.0 / 365)
2535
}
@@ -38,9 +48,16 @@ fn main() {
3848
}
3949
```
4050

51+
<!--
4152
Uncomment the last print statement to observe that the type supplied must be `Years`.
53+
-->
54+
最後の print文 のコメントを外して、与えられた型が `Years` でなければならないことを確認してください。
4255

56+
<!--
4357
To obtain the `newtype`'s value as the base type, you may use the tuple or destructuring syntax like so:
58+
-->
59+
`newtype`の元に使われている型のデータを取得するには、以下のようにタプルや分配構文を用いることで取得できます。
60+
4461
```rust, editable
4562
struct Years(i64);
4663

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy