How to do it...

Perform the following steps:

  1. Create a file named implement.rs and enter the following code in the script:
        use std::{f64};

fn main() {
// create a struct variable
let mut circle1 = Circle {
x:10.0,radius : 10.0
};
println!("x:{},radius : {}", circle1.x,
circle1.radius );
println!("x : {}", circle1.get_x());
}
  1. Create a struct named Circle with two parameters, x and radius:
        // define your custom user data type
struct Circle {
x : f64,
radius : f64,
}
  1. Create the get_x method for the user-defined Circle data type:
        // recommended way of creating structs
impl Circle {
// pub makes this function public which makes it
accessible outsite the scope {}
pub fn get_x(&self) -> f64 {
self.x
}
}

You should get the following screenshot as output upon running the preceding code:

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

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