Advertisement
Guest User

Untitled

a guest
Mar 25th, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.15 KB | None | 0 0
  1. /*
  2. So what I am looking for is the ability to choose
  3. which version of a function is run when it is called
  4. but I cant figure it out.
  5.  
  6. I either want to specificy which version to use
  7. when the function is called or I want to specificy
  8. which version is used when the struct is actually created.
  9.  
  10. Below is an example of what I mean.
  11. */
  12.  
  13. pub struct SimpleMath {
  14. pub x: i32,
  15. pub y: i32,
  16. }
  17.  
  18. impl SimpleMath {
  19. pub fn mul(&self, val: i32) -> i64 {
  20. val as i64 * self.x as i64 * self.y as i64
  21. }
  22. /*
  23. and what I want here is another version of mul with the same signature
  24. that can do something different in the body but still retusn an i64
  25.  
  26. What I have tried is making two new traits that both:
  27.  
  28. impl TRAIT for SimpleMath
  29.  
  30. So when I then make the simple math I could use the trait type. But that
  31. runs into the problem of how do I pass that instance to another function that expects either
  32. version of the trait.
  33.  
  34.  
  35. If this makes very little sense I can make my project repo public and you can look at that instead.
  36.  
  37. */
  38. }
  39.  
  40. fn main() {
  41. let math = SimpleMath { x: 3, y: 4 };
  42. println!("{}", math.mul(4));
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement