Derive

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

The following is a list of derivable traits:

  • Comparison traits: PartialEq
  • Clone, to create a copy of a value
  • Copy, to give a type 'copy semantics' instead of 'move semantics'
  • Drop, to enable moving out of scope
  • Default, to create an empty instance of a data type
  • Debug, to format a value using the {:?} formatter
  • Display, to format a value using the {} formatter
  • Hash, to compute a hash from a value
  • Serde, to serialize and deserialize data structures
  • Store, to enable Starknet storage capabilities
// `Centimeters`, a tuple struct that can be equality-compared
#[derive(Copy, Drop, PartialEq)]
struct Centimeters {
    inner: u64,
}

// `Inches`, a tuple struct that can be printed
#[derive(Copy, Drop, Debug)]
struct Inches {
    inner: u64,
}

#[generate_trait]
impl InchesImpl of InchesTrait {
    fn to_centimeters(self: @Inches) -> Centimeters {
        let Inches { inner: inches } = *self;
        Centimeters {
            inner: inches * 254 / 100,
        } // Convert to centimeters (2.54 * 100 to avoid floats)
    }
}

// `Seconds`, a tuple struct with no additional attributes
#[derive(Drop)]
struct Seconds {
    inner: u64,
}

fn main() {
    let _one_second = Seconds { inner: 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 { inner: 12 };

    println!("One foot equals {:?}", foot);

    let meter = Centimeters { inner: 100 };

    let cmp: ByteArray = if foot.to_centimeters() == meter {
        "equal"
    } else {
        "not equal"
    };

    println!("One foot is {} to one meter.", cmp);
}

See also:

derive