Guest User

Untitled

a guest
Oct 15th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.95 KB | None | 0 0
  1. #![feature(box_syntax)]
  2.  
  3. trait Blorpable: std::fmt::Debug {
  4. fn blorp(&self, n: i32) -> i32;
  5. }
  6.  
  7. #[derive(Debug, Clone)]
  8. struct BlorpAdd(i32);
  9.  
  10. impl Blorpable for BlorpAdd {
  11. fn blorp(&self, n: i32) -> i32 {
  12. self.0 + n
  13. }
  14. }
  15.  
  16. #[derive(Debug, Clone)]
  17. struct BlorpSub(i32);
  18.  
  19. impl Blorpable for BlorpSub {
  20. fn blorp(&self, n: i32) -> i32 {
  21. self.0 - n
  22. }
  23. }
  24.  
  25. #[derive(Debug, Clone)]
  26. struct Blorper<T: Blorpable + Clone>(Vec<Box<T>>);
  27.  
  28. impl<T: Blorpable + Clone> Blorper<T> {
  29. fn blorp_all(&self, n: i32) -> i32 {
  30. self.0.iter().fold(n, |acc, blp| blp.blorp(acc))
  31. }
  32. }
  33.  
  34. impl<T: Blorpable + Clone> Blorpable for Blorper<T> {
  35. fn blorp(&self, n: i32) -> i32 {
  36. self.blorp_all(n)
  37. }
  38. }
  39.  
  40. fn main() {
  41. let blorper = Blorper(vec![
  42. box BlorpAdd(2),
  43. box Blorper(vec![
  44. box BlorpAdd(6),
  45. box BlorpSub(2),
  46. ]),
  47. box BlorpSub(3),
  48. ]);
  49.  
  50. println!("{}", blorper.blorp(10))
  51. }
Add Comment
Please, Sign In to add comment