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/derive.md "Suggest an edit")

#  Derive

The compiler is capable of providing basic implementations for some traits via
the ` #[derive] ` [ attribute ](../attribute.html) . These traits can still be
manually implemented if a more complex behavior is required.

The following is a list of derivable traits:

  * Comparison traits: [ ` Eq ` ](https://doc.rust-lang.org/std/cmp/trait.Eq.html) , [ ` PartialEq ` ](https://doc.rust-lang.org/std/cmp/trait.PartialEq.html) , [ ` Ord ` ](https://doc.rust-lang.org/std/cmp/trait.Ord.html) , [ ` PartialOrd ` ](https://doc.rust-lang.org/std/cmp/trait.PartialOrd.html) . 
  * [ ` Clone ` ](https://doc.rust-lang.org/std/clone/trait.Clone.html) , to create ` T ` from ` &T ` via a copy. 
  * [ ` Copy ` ](https://doc.rust-lang.org/core/marker/trait.Copy.html) , to give a type 'copy semantics' instead of 'move semantics'. 
  * [ ` Hash ` ](https://doc.rust-lang.org/std/hash/trait.Hash.html) , to compute a hash from ` &T ` . 
  * [ ` Default ` ](https://doc.rust-lang.org/std/default/trait.Default.html) , to create an empty instance of a data type. 
  * [ ` Debug ` ](https://doc.rust-lang.org/std/fmt/trait.Debug.html) , to format a value using the ` {:?} ` formatter. 

    
    
    // `Centimeters`, a tuple struct that can be compared
    #[derive(PartialEq, PartialOrd)]
    struct Centimeters(f64);
    
    // `Inches`, a tuple struct that can be printed
    #[derive(Debug)]
    struct Inches(i32);
    
    impl Inches {
        fn to_centimeters(&self) -> Centimeters {
            let &Inches(inches) = self;
    
            Centimeters(inches as f64 * 2.54)
        }
    }
    
    // `Seconds`, a tuple struct with no additional attributes
    struct Seconds(i32);
    
    fn main() {
        let _one_second = Seconds(1);
    
        // Error: `Seconds` can't be printed; it doesn't implement the `Debug` trait
        //println!("One second looks like: {:?}", _one_second);
        // TODO ^ Try uncommenting this line
    
        // Error: `Seconds` can't be compared; it doesn't implement the `PartialEq` trait
        //let _this_is_true = (_one_second == _one_second);
        // TODO ^ Try uncommenting this line
    
        let foot = Inches(12);
    
        println!("One foot equals {:?}", foot);
    
        let meter = Centimeters(100.0);
    
        let cmp =
            if foot.to_centimeters() < meter {
                "smaller"
            } else {
                "bigger"
            };
    
        println!("One foot is {} than one meter.", cmp);
    }
    

###  See also:

[ ` derive ` ](https://doc.rust-lang.org/reference/attributes.html#derive)

[ __ ](../trait.h