11# Clone
22
33When dealing with resources, the default behavior is to transfer them during
4- assignments or function calls. However, sometimes we need to make a
4+ assignments or function calls. However, sometimes we need to make a
55copy of the resource as well.
66
7- The [ ` Clone ` ] [ clone ] trait helps us do exactly this. Most commonly, we can
7+ The [ ` Clone ` ] [ clone ] trait helps us do exactly this. Most commonly, we can
88use the ` .clone() ` method defined by the ` Clone ` trait.
99
1010``` rust,editable
1111// A unit struct without resources
1212#[derive(Debug, Clone, Copy)]
13- struct Nil ;
13+ struct Unit ;
1414
1515// A tuple struct with resources that implements the `Clone` trait
1616#[derive(Clone, Debug)]
1717struct Pair(Box<i32>, Box<i32>);
1818
1919fn main() {
20- // Instantiate `Nil `
21- let nil = Nil ;
22- // Copy `Nil `, there are no resources to move
23- let copied_nil = nil ;
20+ // Instantiate `Unit `
21+ let unit = Unit ;
22+ // Copy `Unit `, there are no resources to move
23+ let copied_unit = unit ;
2424
25- // Both `Nil `s can be used independently
26- println!("original: {:?}", nil );
27- println!("copy: {:?}", copied_nil );
25+ // Both `Unit `s can be used independently
26+ println!("original: {:?}", unit );
27+ println!("copy: {:?}", copied_unit );
2828
2929 // Instantiate `Pair`
3030 let pair = Pair(Box::new(1), Box::new(2));
@@ -37,7 +37,7 @@ fn main() {
3737 // Error! `pair` has lost its resources
3838 //println!("original: {:?}", pair);
3939 // TODO ^ Try uncommenting this line
40-
40+
4141 // Clone `moved_pair` into `cloned_pair` (resources are included)
4242 let cloned_pair = moved_pair.clone();
4343 // Drop the original pair using std::mem::drop
@@ -52,4 +52,4 @@ fn main() {
5252}
5353```
5454
55- [ clone ] : https://doc.rust-lang.org/std/clone/trait.Clone.html
55+ [ clone ] : https://doc.rust-lang.org/std/clone/trait.Clone.html
0 commit comments