Guest User

Untitled

a guest
Dec 15th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.65 KB | None | 0 0
  1. #[derive(Debug)]
  2. struct EuclideanVector<T> {
  3. value: T
  4. }
  5.  
  6. trait Magnitude {
  7. fn magnitude(&self) -> f64;
  8. }
  9.  
  10. impl Magnitude for EuclideanVector<f64> {
  11. fn magnitude(&self) -> f64 {
  12. self.value.sqrt()
  13. }
  14. }
  15.  
  16. impl Magnitude for EuclideanVector<i32> {
  17. fn magnitude(&self) -> f64 {
  18. let val = f64::from(self.value);
  19. val.sqrt()
  20. }
  21. }
  22.  
  23. fn main() {
  24. let x: EuclideanVector<f64> = EuclideanVector {value: 32.5};
  25. let y: EuclideanVector<i32> = EuclideanVector {value: 50};
  26. println!("x: {:?}", x);
  27. println!("y: {:?}", y);
  28. println!("x.magnitude: {}", x.magnitude());
  29. println!("y.magnitude: {}", y.magnitude());
  30. }
Add Comment
Please, Sign In to add comment