pub trait Calculate{
fn calc_area(&self)->u16;
fn calc_peri(&self)->u16;
fn show(&self)->String{
format!(" {} ,Area={},Perimeter={}",self.to_str(),self.calc_area(),self.calc_peri())
}
fn to_str(&self)->String;
}
pub struct Square{
side:u16
}
pub struct Rectangle{
length:u16,
breadth:u16,
}
impl Calculate for Square{
fn calc_area(&self)->u16{
self.side*self.side
}
fn calc_peri(&self)->u16{
4*self.side
}
fn to_str(&self)->String{
format!("For the Square with with side{}",self.side)
}
}
impl Calculate for Rectangle{
fn calc_area(&self)->u16{
self.length*self.breadth
}
fn calc_peri(&self)->u16{
2*(self.length+self.breadth)
}
fn to_str(&self)->String{
format!("For the Rectangle with with length {} breadth {}",self.length,self.breadth)
}
}
fn choice(poly:&impl Calculate){
println!("{}", poly.show());
}
fn main(){
let s=Square{side:3};
let r=Rectangle{length:2,breadth:8};
let mut inp=String::new();
println!("----------MENU--------");
println!("1.Square");
println!("2.Rectangle");
println!("Choose 1/2");
std::io::stdin().read_line(&mut inp).expect("Failed to read");
let ch:u8=inp.trim().parse().expect("Unable to parse");
if ch==1{
choice(&s);
}
else{
choice(&r);
}
}