Skip to content

Commit 4c8e849

Browse files
committed
---
yaml --- r: 224479 b: refs/heads/beta c: 59ff3a3 h: refs/heads/master i: 224477: 4b30eb5 224475: bd4de71 224471: 9ad7236 224463: 0055037 224447: 8248ee9 v: v3
1 parent f882628 commit 4c8e849

File tree

2 files changed

+54
-25
lines changed

2 files changed

+54
-25
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ refs/tags/0.9: 36870b185fc5f5486636d4515f0e22677493f225
2323
refs/tags/0.10: ac33f2b15782272ae348dbd7b14b8257b2148b5a
2424
refs/tags/0.11.0: e1247cb1d0d681be034adb4b558b5a0c0d5720f9
2525
refs/tags/0.12.0: f0c419429ef30723ceaf6b42f9b5a2aeb5d2e2d1
26-
refs/heads/beta: 31adad6aad5b0fbff85a6effcfc5e11ba611493d
26+
refs/heads/beta: 59ff3a39ae866b862678a769f469f0bae55606e5
2727
refs/tags/1.0.0-alpha: e42bd6d93a1d3433c486200587f8f9e12590a4d7
2828
refs/heads/tmp: 938f5d7af401e2d8238522fed4a612943b6e77fd
2929
refs/tags/1.0.0-alpha.2: 4c705f6bc559886632d3871b04f58aab093bfa2f

branches/beta/constructors.md

Lines changed: 53 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,55 @@
11
% Constructors
22

3-
Unlike C++, Rust does not come with a slew of builtin
4-
kinds of constructor. There are no Copy, Default, Assignment, Move, or whatever constructors.
5-
This largely has to do with Rust's philosophy of being explicit.
6-
7-
Move constructors are meaningless in Rust because we don't enable types to "care" about their
8-
location in memory. Every type must be ready for it to be blindly memcopied to somewhere else
9-
in memory. This means pure on-the-stack-but-still-movable intrusive linked lists are simply
10-
not happening in Rust (safely).
11-
12-
Assignment and copy constructors similarly don't exist because move semantics are the *default*
13-
in rust. At most `x = y` just moves the bits of y into the x variable. Rust does provide two
14-
facilities for going back to C++'s copy-oriented semantics: `Copy` and `Clone`. Clone is our
15-
moral equivalent of a copy constructor, but it's never implicitly invoked. You have to explicitly
16-
call `clone` on an element you want to be cloned. Copy is a special case of Clone where the
17-
implementation is just "copy the bits". Copy types *are* implicitly
18-
cloned whenever they're moved, but because of the definition of Copy this just means *not*
19-
treating the old copy as uninitialized -- a no-op.
20-
21-
While Rust provides a `Default` trait for specifying the moral equivalent of a default
22-
constructor, it's incredibly rare for this trait to be used. This is because variables
23-
[aren't implicitly initialized][uninit]. Default is basically only useful for generic
24-
programming. In concrete contexts, a type will provide a static `new` method for any
25-
kind of "default" constructor. This has no relation to `new` in other
26-
languages and has no special meaning. It's just a naming convention.
3+
There is exactly one way to create an instance of a user-defined type: name it,
4+
and initialize all its fields at once:
5+
6+
```rust
7+
struct Foo {
8+
a: u8,
9+
b: u32,
10+
c: bool,
11+
}
12+
13+
enum Bar {
14+
X(u32),
15+
Y(bool),
16+
}
17+
18+
struct Empty;
19+
20+
let foo = Foo { a: 0, b: 1, c: false };
21+
let bar = Bar::X(0);
22+
let empty = Empty;
23+
```
24+
25+
That's it. Every other way you make an instance of a type is just calling a
26+
totally vanilla function that does some stuff and eventually bottoms out to The
27+
One True Constructor.
28+
29+
Unlike C++, Rust does not come with a slew of built in kinds of constructor.
30+
There are no Copy, Default, Assignment, Move, or whatever constructors. The
31+
reasons for this are varied, but it largely boils down to Rust's philosophy
32+
of *being explicit*.
33+
34+
Move constructors are meaningless in Rust because we don't enable types to
35+
"care" about their location in memory. Every type must be ready for it to be
36+
blindly memcopied to somewhere else in memory. This means pure on-the-stack-but-
37+
still-movable intrusive linked lists are simply not happening in Rust (safely).
38+
39+
Assignment and copy constructors similarly don't exist because move semantics
40+
are the *only* semantics in Rust. At most `x = y` just moves the bits of y into the x
41+
variable. Rust *does* provide two facilities for providing C++'s copy-oriented
42+
semantics: `Copy` and `Clone`. Clone is our moral equivalent of a copy
43+
constructor, but it's never implicitly invoked. You have to explicitly call
44+
`clone` on an element you want to be cloned. Copy is a special case of Clone
45+
where the implementation is just "copy the bits". Copy types *are* implicitly
46+
cloned whenever they're moved, but because of the definition of Copy this just
47+
means *not* treating the old copy as uninitialized -- a no-op.
48+
49+
While Rust provides a `Default` trait for specifying the moral equivalent of a
50+
default constructor, it's incredibly rare for this trait to be used. This is
51+
because variables [aren't implicitly initialized][uninit]. Default is basically
52+
only useful for generic programming. In concrete contexts, a type will provide a
53+
static `new` method for any kind of "default" constructor. This has no relation
54+
to `new` in other languages and has no special meaning. It's just a naming
55+
convention.

0 commit comments

Comments
 (0)