Skip to content

Commit f494619

Browse files
committed
derive macro for clone
1 parent dbe9cd1 commit f494619

File tree

2 files changed

+37
-14
lines changed

2 files changed

+37
-14
lines changed

.icarukTime.json

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
{
2-
"idleHeartbeats": 14388,
3-
"navigatingHeartbeats": 2998,
4-
"totalCodingHeartbeats": 18674,
2+
"idleHeartbeats": 14588,
3+
"navigatingHeartbeats": 3016,
4+
"totalCodingHeartbeats": 19114,
55
"languageHeartbeats": {
66
"jsonc": 47,
77
"toml": 3591,
8-
"markdown": 10663,
8+
"markdown": 10925,
99
"html": 3210,
10-
"scminput": 933,
10+
"scminput": 1066,
1111
"yaml": 17,
1212
"plaintext": 48,
13-
"Log": 165
13+
"Log": 210
1414
},
1515
"fileHeartbeats": {
1616
".vscode/settings.json": 47,
@@ -51,9 +51,9 @@
5151
"content/test/triangle/index.md": 264,
5252
"content/test/pentagone/index.html": 45,
5353
"content/test/pentagone/index.md": 232,
54-
"git/scm0/input": 101,
54+
"git/scm0/input": 234,
5555
"layouts/shortcodes/qr.html": 92,
56-
"content/posts/constructor_in_rust/index.md": 78,
57-
"extension-output-mkxml.vscode-filesize-#1-filesize": 165
56+
"content/posts/constructor_in_rust/index.md": 340,
57+
"extension-output-mkxml.vscode-filesize-#1-filesize": 210
5858
}
5959
}

content/posts/constructor_in_rust/index.md

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,27 +31,50 @@ Primitive types like integers or floats already have a native syntax for constru
3131

3232
## Move Semantic: Copy and Clone
3333

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:
34+
In Rust, there are 2 traits for duplicating a value: `Clone` and `Copy`.
3535
- `Clone`: [Create a deep, independent copy of the value](<https://doc.rust.org/std/clone/trait.Clone.html>).
3636

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`.
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 implies `Clone`.
38+
By *marker* trait, I mean there is no logic in the `Copy` trait itself the definiton is: `pub trait Copy: Clone { }`. The compiler ensure that the type implements `Clone` and that it can be bit copied. All the logic for duplicating a value is in the `Clone` trait.
39+
40+
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`.
3941

4042

4143
`Copy` constructor are done implicitly, and are fast..
4244

4345
```rs
4446
let value = 42;
45-
let value2 = value; // implicit copy
47+
let value2 = value; // implicit copy because it is cheap to do.
4648
```
4749

4850
...and `Clone` constructor are done explicitly, and are slower because the object is generaly heavier to duplicate.
4951

5052
```rs
51-
let value = "hello".to_owned(); // 1 memory allocation, can't be bit copied
53+
let value = "hello".to_owned(); // 1 memory allocation, and can't be bit copied
5254
let value2 = value.clone(); // explicit clone, 2 memory allocations
5355
```
5456

57+
For comparison, everythings is implictely cloned in C++, there no distinction between `Copy` and `Clone`.
58+
This mean you can accidentally clone a vector or a string, which can be expensive, without realizing it.
59+
60+
You can impl manually the `Clone` trait for a type, or you can use the `derive macro` that will implement it automatically for you:
61+
62+
```rs
63+
// derive macro:
64+
#[derive(Clone, Copy)]
65+
struct MyI32 {
66+
x: i32
67+
}
68+
69+
// or manually:
70+
impl Clone for MyI32 {
71+
fn clone(&self) -> Self {
72+
MyI32 { x: self.x }
73+
}
74+
}
75+
impl Copy for MyI32 {}
76+
```
77+
5578
## Struct Instantiation in Rust
5679

5780
```rs

0 commit comments

Comments
 (0)