1
+ <!--
1
2
# Constructors
3
+ -->
2
4
5
+ # コンストラクタ
6
+
7
+ <!--
3
8
There is exactly one way to create an instance of a user-defined type: name it,
4
9
and initialize all its fields at once:
10
+ -->
11
+
12
+ ユーザが定義した型のインスタンスを作る方法はただ一つしかありません: 型名を決めて、
13
+ 全てのフィールドをいっぺんに初期化することです。
5
14
6
15
``` rust
7
16
struct Foo {
@@ -22,20 +31,41 @@ let bar = Bar::X(0);
22
31
let empty = Unit ;
23
32
```
24
33
34
+ <!--
25
35
That's it. Every other way you make an instance of a type is just calling a
26
36
totally vanilla function that does some stuff and eventually bottoms out to The
27
37
One True Constructor.
38
+ -->
28
39
40
+ 以上。これ以外の型のインスタンスを作る方法は皆、単にいくつかのことを行なう全く普通の
41
+ 関数を呼び、結局 1 つの真のコンストラクタに辿り着くのです。
42
+
43
+ <!--
29
44
Unlike C++, Rust does not come with a slew of built-in kinds of constructor.
30
45
There are no Copy, Default, Assignment, Move, or whatever constructors. The
31
46
reasons for this are varied, but it largely boils down to Rust's philosophy of
32
47
*being explicit*.
48
+ -->
49
+
50
+ C++ と違い、 Rust は沢山の組み込みコンストラクタを備えていません。
51
+ Rust には、 Copy、 Default、 Assignment、 Moveやその他諸々のコンストラクタが
52
+ ありません。理由は様々ですが、大体 Rust の考え方である、* 明確であること* 、という事に
53
+ 落ち着きます。
33
54
55
+ <!--
34
56
Move constructors are meaningless in Rust because we don't enable types to
35
57
"care" about their location in memory. Every type must be ready for it to be
36
58
blindly memcopied to somewhere else in memory. This means pure on-the-stack-but-
37
59
still-movable intrusive linked lists are simply not happening in Rust (safely).
60
+ -->
61
+ Move コンストラクタは Rust においては意味がありません。なぜなら、型が、自身の
62
+ メモリ上の場所を "気にする" ようにはしないからです。すべての型は何もしなくても、
63
+ メモリ中のどこか別の場所にコピー出来るよう準備されなければなりません。
64
+ これは、純粋な、スタック上にあるけれどもそれでも動かすことの出来る、
65
+ あるノードの次のノードへのポインタをそのノード自身が保持する線形リストは (安全には) 存在し得ない
66
+ 事を意味します。
38
67
68
+ <!--
39
69
Assignment and copy constructors similarly don't exist because move semantics
40
70
are the only semantics in Rust. At most `x = y` just moves the bits of y into
41
71
the x variable. Rust does provide two facilities for providing C++'s copy-
@@ -45,15 +75,40 @@ constructor, but it's never implicitly invoked. You have to explicitly call
45
75
where the implementation is just "copy the bits". Copy types *are* implicitly
46
76
cloned whenever they're moved, but because of the definition of Copy this just
47
77
means not treating the old copy as uninitialized -- a no-op.
78
+ -->
48
79
80
+ Assignment コンストラクタや Copy コンストラクタも同様に存在しません。
81
+ なぜなら、ムーブセマンティクスは Rust における唯一のセマンティクスだからです。
82
+ せいぜい ` x = y ` が単に y のビットを変数 x に移すくらいです。 Rust では C++ の
83
+ コピー指向のセマンティクスを提供する、 2 つの機能があります。 ` Copy ` と ` Clone ` です。 Clone は Copy コンストラクタと
84
+ 同じようなものですが、暗黙に呼び出されることは一切ありません。クローンを生成したい要素に対して、
85
+ 明示的に ` clone ` を呼び出す必要があります。 Copy は Clone の特別なケースで、
86
+ 実装は単純に "ビットをコピーする" ことです。 Copy を実装する型は、
87
+ ムーブが発生すると毎回クローンを生成* します* 。しかし、 Copy の定義によって、
88
+ これは、古いコピーを初期化されていないとは扱わない事を単に意味します。つまり no-op なのです。
89
+
90
+ <!--
49
91
While Rust provides a `Default` trait for specifying the moral equivalent of a
50
92
default constructor, it's incredibly rare for this trait to be used. This is
51
93
because variables [aren't implicitly initialized][uninit]. Default is basically
52
94
only useful for generic programming. In concrete contexts, a type will provide a
53
95
static `new` method for any kind of "default" constructor. This has no relation
54
96
to `new` in other languages and has no special meaning. It's just a naming
55
97
convention.
98
+ -->
99
+
100
+ Rust は Default コンストラクタと同等のものを指定する、 ` Default ` トレイトを
101
+ 提供していますが、このトレイトが使用されるのは驚くほど稀です。なぜなら、
102
+ 変数は[ 暗黙には初期化されない] [ uninit ] からです。 Default は、
103
+ 基本的にはジェネリックプログラミングでのみ有用です。具体例では、
104
+ あらゆる種類の "デフォルトの" コンストラクタに対して、このトレイトを実装する型が静的な ` new ` メソッドを
105
+ 提供します。これは他の言語における ` new ` とは関係がなく、特に意味はありません。
106
+ これはただの命名規則です。
56
107
108
+ <!--
57
109
TODO: talk about "placement new"?
110
+ -->
111
+
112
+ TODO: "placement new" について話す?
58
113
59
114
[ uninit ] : uninitialized.html
0 commit comments