Rust - The #[derive] Attribute Last Updated : 26 Sep, 2022 Comments Improve Suggest changes Like Article Like Report In Rust, we come across the concept of the #[derive] attribute. Derive attribute is used for generating data structures automatically by using MetalistPaths syntax for specifying the traits that help in implementing paths to derive macros for processing. Expression of the trait is: #[derive(PartialEq, Clone)]struct Foo<T> { a: i32, b: T,} In this example, we are comparing a struct and printing a tuple. Here, we are comparing the traits Eq, PartialEq, and the PartialOrd trait. Example 1: Rust // Rust program for The #[derive] attribute // `GFGStruct`, a tuple struct that can be compared #[derive(PartialEq, PartialOrd)] struct GFGStruct(f64); // `GFGTuple, a tuple struct that can be printed #[derive(Debug)] struct GFG(i32); impl GFG { fn parameter_one(&self) -> GFGStruct { let &GFG(variable) = self; GFGStruct(variable as f64 * 8.00) } } fn main() { let firstvariable = GFG(1); println!("Lets learn rust from {:?}", firstvariable); let secondvariable = GFGStruct(50.0); let thirdvariable = if firstvariable.parameter_one() < secondvariable { "first variable" } else { "Second variable" }; println!("{} gets printed on screen.", thirdvariable); } Output: The #derive attribute can be explained using this example. We create a tuple known as GFGStruct and import the #derive(PartialEq, PartialOrd) attributes. GFGStruct is used for comparing and GFGTuple is created so that it can be printed. Next, we create an impl GFG where we pass a parameter named variable. This variable is passed on to the GFGStruct as a floating point variable. Now, we compare the first variable and the second variable that we had declared in the main function. In the variables, we pass a floating value and an integer value in the GFG impl. After passing, we compare the variables by declaring the third variable and we clearly see that 1<50. Therefore, we see that the third variable gets the value of the first variable as the first variable.parameter_one < second variable Comment More infoAdvertise with us Next Article Rust - The #[derive] Attribute S sayanc170 Follow Improve Article Tags : Rust Rust attributes Similar Reads Rust - Crate_type Attribute In Rust, we have a concept of crates. Crates are basically libraries or packages as defined in other programming languages. Cargo, the rust package management tool helps ship the crates of our program to other programs' cargo.  Crates produce an executable library where every crate has a root module 2 min read Rust - The newtype Idiom In Rust, we have a concept of "newtype idiom" that is categorized under Rust generics. The newtype idiom is a design pattern in Rust that is responsible for ensuring safe coding practices and also ensures that our code is type-safe. The main function of newtype idiom is ensuring that the right value 2 min read Rust - Deref Trait Deref<T> trait in Rust is used for customizing dereferencing operator (*) behavior. Implementing the Deref <T> treats the smart pointer as a reference. Therefore, any code that references the deref trait would also refer to smart pointers. Regular References in Deref Trait:Regular refer 2 min read Rust - Result Type In Rust, we have a result-type operator that is used for error-handling operations with the result-type operator. The Result type is wrapped in an enum that has two values - Ok and Err. Ok (T) denotes success while Err(E) is used for representing errors and error values (if present). While working 3 min read Rust Types and Inference Pre-requisites: Rust, Scalar Datatypes in Rust Rust is a multi-paradigm programming language like C++ syntax that was designed for performance and safety, especially safe concurrency by using a borrow checker and ownership to validate references. In this article, we will focus on Rust's primitive t 12 min read Rust - The dyn Keyword In Rust, we have dyn Keyword. We know that Struct is implemented via a trait by using the impl keyword and its own definitions are specified by using the trait methods. Dyn keyword is generally used as a trait for the object's type and is used for highlighting the methods by which the traits are dis 3 min read Rust - Formatting Print Statements In Rust, we use macros to print formatted texts in the editor. Macros provide functionality similar to functions but without the runtime cost. Print statements are used extensively in programming. They are used for prompting users for information or for debugging the program. Macros in Rust end with 3 min read Rust lifetime Constructor In Rust, we have a concept of a lifetime constructor that falls under the ownership category. Rust enforces a certain set of rules through its concept of ownership and lifetimes. In this article, we would be learning more about Lifetimes in Rust. In Rust, there are various regions that are present i 3 min read Rust - The mut Modifier In Rust, we have a concept of mut modifier.  A mutable variable is a variable used for binding values to variable names. Mut variables in Rust have specific use cases i.e. used in situations where we need to bind values into variables. For declaring a variable as mutable, we use the mut keyword as v 2 min read Rust - Multiple Error Types In Rust, sometimes we have scenarios where there is more than one error type. In Rust, the way of handling errors is different compared to other Object Oriented / System languages. Rust specifically uses 'Result' for returning something. The Result <T, E> is basically an enum that has - Ok(T) 3 min read Like