misc/process/wait.html)
    9. [ **20.6.** Filesystem Operations ](../std_misc/fs.html)
    10. [ **20.7.** Program arguments ](../std_misc/arg.html) â± 
    11.       1. [ **20.7.1.** Argument parsing ](../std_misc/arg/matching.html)
    12. [ **20.8.** Foreign Function Interface ](../std_misc/ffi.html)
  41. [ **21.** Testing ](../testing.html) â± 
  42.     1. [ **21.1.** Unit testing ](../testing/unit_testing.html)
    2. [ **21.2.** Documentation testing ](../testing/doc_testing.html)
    3. [ **21.3.** Integration testing ](../testing/integration_testing.html)
    4. [ **21.4.** Dev-dependencies ](../testing/dev_dependencies.html)
  43. [ **22.** Unsafe Operations ](../unsafe.html) â± 
  44.     1. [ **22.1.** Inline assembly ](../unsafe/asm.html)
  45. [ **23.** Compatibility ](../compatibility.html) â± 
  46.     1. [ **23.1.** Raw identifiers ](../compatibility/raw_identifiers.html)
  47. [ **24.** Meta ](../meta.html) â± 
  48.     1. [ **24.1.** Documentation ](../meta/doc.html)
    2. [ **24.2.** Playground ](../meta/playground.html)

__ __

  * Light (default) 
  * Rust 
  * Coal 
  * Navy 
  * Ayu 

__

#  Rust By Example

[ __ ](../print.html "Print this book") [ __ ](https://github.com/rust-
lang/rust-by-example "Git repository") [ __ ](https://github.com/rust-
lang/rust-by-example/edit/master/src/trait/clone.md "Suggest an edit")

#  Clone

When dealing with resources, the default behavior is to transfer them during
assignments or function calls. However, sometimes we need to make a copy of
the resource as well.

The [ ` Clone ` ](https://doc.rust-lang.org/std/clone/trait.Clone.html) trait
helps us do exactly this. Most commonly, we can use the ` .clone() ` method
defined by the ` Clone ` trait.

    
    
    // A unit struct without resources
    #[derive(Debug, Clone, Copy)]
    struct Unit;
    
    // A tuple struct with resources that implements the `Clone` trait
    #[derive(Clone, Debug)]
    struct Pair(Box<i32>, Box<i32>);
    
    fn main() {
        // Instantiate `Unit`
        let unit = Unit;
        // Copy `Unit`, there are no resources to move
        let copied_unit = unit;
    
        // Both `Unit`s can be used independently
        println!("original: {:?}", unit);
        println!("copy: {:?}", copied_unit);
    
        // Instantiate `Pair`
        let pair = Pair(Box::new(1), Box::new(2));
        println!("original: {:?}", pair);
    
        // Move `pair` into `moved_pair`, moves resources
        let moved_pair = pair;
        println!("moved: {:?}", moved_pair);
    
        // Error! `pair` has lost its resources
        //println!("original: {:?}", pair);
        // TODO ^ Try uncommenting this line
    
        // Clone `moved_pair` into `cloned_pair` (resources are included)
        let cloned_pair = moved_pair.clone();
        // Drop the original pair using std::mem::drop
        drop(moved_pair);
    
        // Error! `moved_pair` has been dropped
        //println!("copy: {:?}", moved_pair);
        // TODO ^ Try uncommenting this line
    
        // The result from .clone() can still be used!
        println!("clone: {:?}", cloned_pair);
    }
    

[ __ ](../trait/impl_trait.html "Previous chapter") [ __
](../trait/supertraits.html "Next chapter")

[ __ ](../trait/impl_trait.html "Previous chapter") [ __
](../trait/supertraits.html "Next chapter")