Derive macro
The derive macro is added to a type definition to provide it with some basic implementations. The derive macro is used to implement traits for structs and enums automatically. The compiler will generate implementations for the requested traits. Note this is supported for generic types as well.
#[derive(Copy, Drop, PartialEq)]
struct Foo {
x: i32,
y: i32,
}
#[derive(Copy)]
struct FeltAndT<T, impl TCopy: Copy<T>> {
f: felt252,
t: T,
}
Existing derived traits
-
Copy
- Marks the type as being safe to copy by simply copying bits. All members of the type must also beCopy
. See linear types for more information. -
Clone
- Implements the clone method to explicitly create a copy of the type. All members of the type must also beClone
. See linear types for more information. -
Drop
- Marks the type as being safe to drop. All members of the type must also beDrop
. See linear types for more information. -
Destruct
- Implements the destruct method to explicitly destroy the type. All members of the type must also beDestruct
. See linear types for more information. -
PartialEq
- Implements the equality operators==
and!=
for the type. All members of the type must also bePartialEq
. -
Serde
- Implements the Serde trait for serialization and deserialization for the type. All members of the type must also beSerde
.