Skip to content

Commit 8ca3f99

Browse files
committed
---
yaml --- r: 228991 b: refs/heads/try c: 59ff3a3 h: refs/heads/master i: 228989: b3ac14d 228987: 43697dd 228983: 16e0a69 228975: 67425d9 228959: 984c7bf 228927: bae3316 228863: 1fec5f7 v: v3
1 parent 519c3e4 commit 8ca3f99

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
@@ -1,7 +1,7 @@
11
---
22
refs/heads/master: aca2057ed5fb7af3f8905b2bc01f72fa001c35c8
33
refs/heads/snap-stage3: 1af31d4974e33027a68126fa5a5a3c2c6491824f
4-
refs/heads/try: 31adad6aad5b0fbff85a6effcfc5e11ba611493d
4+
refs/heads/try: 59ff3a39ae866b862678a769f469f0bae55606e5
55
refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105
66
refs/tags/release-0.2: c870d2dffb391e14efb05aa27898f1f6333a9596
77
refs/tags/release-0.3: b5f0d0f648d9a6153664837026ba1be43d3e2503

branches/try/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)