1- # Clone
1+ # Clone and Copy
22
33When dealing with resources, the default behavior is to transfer them during
44assignments or function calls. However, sometimes we need to make a
@@ -7,19 +7,37 @@ copy of the resource as well.
77The [ ` Clone ` ] [ clone ] trait helps us do exactly this. Most commonly, we can
88use the ` .clone() ` method defined by the ` Clone ` trait.
99
10+ ## Copy: Implicit Cloning
11+
12+ The [ ` Copy ` ] [ copy ] trait allows a type to be duplicated simply by copying bits,
13+ with no additional logic required. When a type implements ` Copy ` , assignments
14+ and function calls will implicitly copy the value instead of moving it.
15+
16+ ** Important:** ` Copy ` requires ` Clone ` - any type that implements ` Copy ` must
17+ also implement ` Clone ` . This is because ` Copy ` is defined as a subtrait:
18+ ` trait Copy: Clone {} ` . The ` Clone ` implementation for ` Copy ` types simply
19+ copies the bits.
20+
21+ Not all types can implement ` Copy ` . A type can only be ` Copy ` if:
22+ - All of its components are ` Copy `
23+ - It doesn't manage external resources (like heap memory, file handles, etc.)
24+
1025``` rust,editable
1126// A unit struct without resources
27+ // Note: Copy requires Clone, so we must derive both
1228#[derive(Debug, Clone, Copy)]
1329struct Unit;
1430
1531// A tuple struct with resources that implements the `Clone` trait
32+ // This CANNOT be Copy because Box<T> is not Copy
1633#[derive(Clone, Debug)]
1734struct Pair(Box<i32>, Box<i32>);
1835
1936fn main() {
2037 // Instantiate `Unit`
2138 let unit = Unit;
22- // Copy `Unit`, there are no resources to move
39+ // Copy `Unit` - this is an implicit copy, not a move!
40+ // Because Unit implements Copy, the value is duplicated automatically
2341 let copied_unit = unit;
2442
2543 // Both `Unit`s can be used independently
@@ -31,6 +49,7 @@ fn main() {
3149 println!("original: {:?}", pair);
3250
3351 // Move `pair` into `moved_pair`, moves resources
52+ // Pair does not implement Copy, so this is a move
3453 let moved_pair = pair;
3554 println!("moved: {:?}", moved_pair);
3655
@@ -39,6 +58,7 @@ fn main() {
3958 // TODO ^ Try uncommenting this line
4059
4160 // Clone `moved_pair` into `cloned_pair` (resources are included)
61+ // Unlike Copy, Clone is explicit - we must call .clone()
4262 let cloned_pair = moved_pair.clone();
4363 // Drop the moved original pair using std::mem::drop
4464 drop(moved_pair);
@@ -53,3 +73,4 @@ fn main() {
5373```
5474
5575[ clone ] : https://doc.rust-lang.org/std/clone/trait.Clone.html
76+ [ copy ] : https://doc.rust-lang.org/std/marker/trait.Copy.html
0 commit comments