How it works...

In this recipe, we applied all the concepts that we learned in the previous ones. We created two struct types: Circle with a radius of f64 and Rectangle with the parameters h and b of f64. Then, we created the area functionality for each struct data type that operates on the data of the labels, as they are referenced by self.

The function definition of both user-defined data types is different in terms of the mathematical operation. We defined the data type Circle and Rectangle in the main function. We called the functions in real time by Circle.area() and Rectangle.area().

Here, we observe that both data types provide a similar kind of functionality; this is where the trait comes into place. It basically tells the compiler the functionality that a particular function would use, so we implement the trait. For the data type in this recipe, we have a trait named HasArea, which contains only the signature of the function that is inside the scope, which contains the output that is returned and the reference which was passed as the argument. In this recipe, we had a signature of fn area(&self) -> f64;, which indicated the output of the computation in a 64-bit float type. The function operates by taking a reference to the label and values of the data type.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.61.170