I'm new to Rust and currently reading about structs. The code below is just a simple test of mine and I was wondering how to allow any numeric value as a property r of the Circle structure. I want it to be able to store an integer or a float.
I would assume overloading but not sure. Also from what I've searched online there are some crates num and num-traits but I'm unsure how to use them, unless I missed the doscs section.
use std::f64::consts::PI;
trait Area {
fn calculate_area(self) -> f64;
}
struct Circle {
r:f64
}
impl Area for Circle {
fn calculate_area(self) -> f64 {
PI * self.r * self.r
}
}
fn main() {
let c = Circle { r: 2.5 };
println!("The area is {}", c.calculate_area())
}
I would prefer a solution without having to include another crate.