Solution : Find the Area of a Triangle
fn my_area(x: i32 , y: i32 )-> f32{ // root function
( x as f32 ) * ( y as f32 ) * 0.5 // compute area of triangle
}
// declare a module
mod shapes {
// function within outer module
pub fn triangle_area(x : i32 , y : i32) {
println!("{}", super :: my_area ( x , y )); // invoke the root function
}
}
fn main(){
print!("Area of triangle with width = 3 and height = 4 : ");
shapes::triangle_area(3, 4);
print!("Area of triangle with width = 9 and height = 4 : ");
shapes::triangle_area(9, 4);
}
output
Area of triangle with width = 3 and height = 4 : 6
Area of triangle with width = 9 and height = 4 : 18