Perform the following steps:
- 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());
}
- Create a struct named Circle with two parameters, x and radius:
// define your custom user data type
struct Circle {
x : f64,
radius : f64,
}
- 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: