Skip to content

Commit c1868a1

Browse files
authored
Translate constructors.md (#43)
1 parent aeda246 commit c1868a1

File tree

2 files changed

+56
-1
lines changed

2 files changed

+56
-1
lines changed

src/SUMMARY.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
* [ドロップフラグ](drop-flags.md)
3131
* [チェックされないメモリ](unchecked-uninit.md)
3232
* [所有権に基づいたリソース管理](obrm.md)
33-
* [Constructors](constructors.md)
33+
* [コンストラクタ](constructors.md)
3434
* [Destructors](destructors.md)
3535
* [Leaking](leaking.md)
3636
* [Unwinding](unwinding.md)

src/constructors.md

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,16 @@
1+
<!--
12
# Constructors
3+
-->
24

5+
# コンストラクタ
6+
7+
<!--
38
There is exactly one way to create an instance of a user-defined type: name it,
49
and initialize all its fields at once:
10+
-->
11+
12+
ユーザが定義した型のインスタンスを作る方法はただ一つしかありません: 型名を決めて、
13+
全てのフィールドをいっぺんに初期化することです。
514

615
```rust
716
struct Foo {
@@ -22,20 +31,41 @@ let bar = Bar::X(0);
2231
let empty = Unit;
2332
```
2433

34+
<!--
2535
That's it. Every other way you make an instance of a type is just calling a
2636
totally vanilla function that does some stuff and eventually bottoms out to The
2737
One True Constructor.
38+
-->
2839

40+
以上。これ以外の型のインスタンスを作る方法は皆、単にいくつかのことを行なう全く普通の
41+
関数を呼び、結局 1 つの真のコンストラクタに辿り着くのです。
42+
43+
<!--
2944
Unlike C++, Rust does not come with a slew of built-in kinds of constructor.
3045
There are no Copy, Default, Assignment, Move, or whatever constructors. The
3146
reasons for this are varied, but it largely boils down to Rust's philosophy of
3247
*being explicit*.
48+
-->
49+
50+
C++ と違い、 Rust は沢山の組み込みコンストラクタを備えていません。
51+
Rust には、 Copy、 Default、 Assignment、 Moveやその他諸々のコンストラクタが
52+
ありません。理由は様々ですが、大体 Rust の考え方である、*明確であること*、という事に
53+
落ち着きます。
3354

55+
<!--
3456
Move constructors are meaningless in Rust because we don't enable types to
3557
"care" about their location in memory. Every type must be ready for it to be
3658
blindly memcopied to somewhere else in memory. This means pure on-the-stack-but-
3759
still-movable intrusive linked lists are simply not happening in Rust (safely).
60+
-->
61+
Move コンストラクタは Rust においては意味がありません。なぜなら、型が、自身の
62+
メモリ上の場所を "気にする" ようにはしないからです。すべての型は何もしなくても、
63+
メモリ中のどこか別の場所にコピー出来るよう準備されなければなりません。
64+
これは、純粋な、スタック上にあるけれどもそれでも動かすことの出来る、
65+
あるノードの次のノードへのポインタをそのノード自身が保持する線形リストは (安全には) 存在し得ない
66+
事を意味します。
3867

68+
<!--
3969
Assignment and copy constructors similarly don't exist because move semantics
4070
are the only semantics in Rust. At most `x = y` just moves the bits of y into
4171
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
4575
where the implementation is just "copy the bits". Copy types *are* implicitly
4676
cloned whenever they're moved, but because of the definition of Copy this just
4777
means not treating the old copy as uninitialized -- a no-op.
78+
-->
4879

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+
<!--
4991
While Rust provides a `Default` trait for specifying the moral equivalent of a
5092
default constructor, it's incredibly rare for this trait to be used. This is
5193
because variables [aren't implicitly initialized][uninit]. Default is basically
5294
only useful for generic programming. In concrete contexts, a type will provide a
5395
static `new` method for any kind of "default" constructor. This has no relation
5496
to `new` in other languages and has no special meaning. It's just a naming
5597
convention.
98+
-->
99+
100+
Rust は Default コンストラクタと同等のものを指定する、 `Default` トレイトを
101+
提供していますが、このトレイトが使用されるのは驚くほど稀です。なぜなら、
102+
変数は[暗黙には初期化されない][uninit]からです。 Default は、
103+
基本的にはジェネリックプログラミングでのみ有用です。具体例では、
104+
あらゆる種類の "デフォルトの" コンストラクタに対して、このトレイトを実装する型が静的な `new` メソッドを
105+
提供します。これは他の言語における `new` とは関係がなく、特に意味はありません。
106+
これはただの命名規則です。
56107

108+
<!--
57109
TODO: talk about "placement new"?
110+
-->
111+
112+
TODO: "placement new" について話す?
58113

59114
[uninit]: uninitialized.html

0 commit comments

Comments
 (0)