You will learn about the trait
feature of Rust in this recipe is similar to impl
, which helps the developer make a method call of the user-defined data type. However, trait
provides many more features, such as inheritance and control, over the functionality that the user-defined data type provides.
Perform the following steps:
- Create a file named
trait.rs
and enter the following code in the script:
use std::{f64}; fn main() { // variable of circle data type let mut circle1 = Circle { r : 10.0 }; println!("Area of circle {}", circle1.area() ); // variable of rectangle data type let mut rect = Rectangle { h:10.0,b : 10.0 }; println!("Area of rectangle {}", rect.area() ); }
- Create a
struct
namedRectangle
with the parametersh
andb
, both 64-bit...