struct Val<T> {
val: T,
}
impl<T> Val<T> {
fn value(&self) -> &T {
&self.val
}
}
fn main() {
let x = Val{ val: 3.0 };
let y = Val{ val: "hello".to_string()};
println!("{}, {}", x.value(), y.value());
}
========================================================================
struct Point<T> {
x: T,
y: T,
}
impl Point<f32> {
fn distance_from_origin(&self) -> f32 {
(&self.x.powi(2) + &self.y.powi(2)).sqrt()
}
}
fn main() {
let p = Point{x: 5.0_f32, y: 10.0_f32};
println!("{}",p.distance_from_origin())
}