Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #[derive(Debug)]
- struct EuclideanVector<T> {
- value: T
- }
- trait Magnitude {
- fn magnitude(&self) -> f64;
- }
- impl Magnitude for EuclideanVector<f64> {
- fn magnitude(&self) -> f64 {
- self.value.sqrt()
- }
- }
- impl Magnitude for EuclideanVector<i32> {
- fn magnitude(&self) -> f64 {
- let val = f64::from(self.value);
- val.sqrt()
- }
- }
- fn main() {
- let x: EuclideanVector<f64> = EuclideanVector {value: 32.5};
- let y: EuclideanVector<i32> = EuclideanVector {value: 50};
- println!("x: {:?}", x);
- println!("y: {:?}", y);
- println!("x.magnitude: {}", x.magnitude());
- println!("y.magnitude: {}", y.magnitude());
- }
Add Comment
Please, Sign In to add comment