Advertisement
Guest User

Untitled

a guest
Sep 17th, 2019
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.65 KB | None | 0 0
  1. struct Calculator<C>
  2. where C: Fn(u32, u32) -> u32
  3. {
  4. context: (u32, u32),
  5. calculate: C,
  6. }
  7.  
  8. impl<C> Calculator<C>
  9. where C: Fn(u32, u32) -> u32
  10. {
  11. fn new(lhs: u32, rhs: u32, calculate: C) -> Self
  12. {
  13. Self { context: (lhs, rhs), calculate }
  14. }
  15.  
  16. fn execute(&self) -> u32
  17. {
  18. let (lhs, rhs) = self.context;
  19. (self.calculate)(lhs, rhs)
  20. }
  21. }
  22.  
  23. fn main()
  24. {
  25. let c1 = Calculator::new(12, 33, |l, r| l + r);
  26. let c2 = Calculator::new(3, 5, |l, r| l*r);
  27.  
  28. let x = 12;
  29. let c3 = Calculator::new(66, 4, |l, r| l + x + r);
  30.  
  31. dbg!(c1.execute());
  32. dbg!(c2.execute());
  33. dbg!(c3.execute());
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement