Skip to content

Commit dbe9cd1

Browse files
committed
move semantic
1 parent 4f27155 commit dbe9cd1

File tree

2 files changed

+31
-5
lines changed

2 files changed

+31
-5
lines changed

.icarukTime.json

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
{
22
"idleHeartbeats": 14388,
3-
"navigatingHeartbeats": 2978,
4-
"totalCodingHeartbeats": 18509,
3+
"navigatingHeartbeats": 2998,
4+
"totalCodingHeartbeats": 18674,
55
"languageHeartbeats": {
66
"jsonc": 47,
77
"toml": 3591,
88
"markdown": 10663,
99
"html": 3210,
1010
"scminput": 933,
1111
"yaml": 17,
12-
"plaintext": 48
12+
"plaintext": 48,
13+
"Log": 165
1314
},
1415
"fileHeartbeats": {
1516
".vscode/settings.json": 47,
@@ -52,6 +53,7 @@
5253
"content/test/pentagone/index.md": 232,
5354
"git/scm0/input": 101,
5455
"layouts/shortcodes/qr.html": 92,
55-
"content/posts/constructor_in_rust/index.md": 78
56+
"content/posts/constructor_in_rust/index.md": 78,
57+
"extension-output-mkxml.vscode-filesize-#1-filesize": 165
5658
}
5759
}

content/posts/constructor_in_rust/index.md

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,30 @@ Primitive types like integers or floats already have a native syntax for constru
2828

2929
- **Placement** : In C++, the constructor also determines where in memory the object is initialized.
3030

31+
32+
## Move Semantic: Copy and Clone
33+
34+
In Rust, there are two traits that can be automatically implemented for a type: `Clone` and `Copy`. I don't want to enter to much details about the difference between the two, but here are the main differences:
35+
- `Clone`: [Create a deep, independent copy of the value](<https://doc.rust.org/std/clone/trait.Clone.html>).
36+
37+
- `Copy`: [Types whose values can be duplicated simply by copying bits.](<https://doc.rust.org/std/marker/trait.Copy.html>) is a marker trait that imply `Clone`.
38+
Type that have have a field that have an indirection layer in memory such as `Box`, `Vec`, `String`, `HashMap`, `HashSet`, etc can't be `Copy`, just `Clone`.
39+
40+
41+
`Copy` constructor are done implicitly, and are fast..
42+
43+
```rs
44+
let value = 42;
45+
let value2 = value; // implicit copy
46+
```
47+
48+
...and `Clone` constructor are done explicitly, and are slower because the object is generaly heavier to duplicate.
49+
50+
```rs
51+
let value = "hello".to_owned(); // 1 memory allocation, can't be bit copied
52+
let value2 = value.clone(); // explicit clone, 2 memory allocations
53+
```
54+
3155
## Struct Instantiation in Rust
3256

3357
```rs
@@ -62,7 +86,7 @@ let alice = Person { name: "Alice".to_owned(), .. bobby };
6286

6387
dbg!(&bobby.name); // ok because Alice constructor has her own name
6488
// dbg!(&bobby.hobbies); // error: borrow of moved value: `bobby.hobbies`. It was moved to Alice
65-
dbg!(&bobby.age); // ok, because the type `i32` is Copy, so it was copied, not moved
89+
dbg!(&bobby.age); // ok, because the type `i32` is `Copy`, so it was copied, not moved
6690
```
6791

6892
If you still want to be able to use bobby, one easy way is to clone it:

0 commit comments

Comments
 (0)