SHARE
TWEET

Untitled

a guest Sep 23rd, 2019 72 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. trait OneAdder {
  2.     fn add_one(&mut self);
  3. }
  4.  
  5. trait AnyAdder {
  6.     fn add_any(&mut self, extra: u32);
  7. }
  8.  
  9. impl<T:OneAdder> AnyAdder for T  {
  10.     fn add_any(&mut self, value: u32) {
  11.         for _ in 0..value {
  12.             self.add_one()
  13.         }
  14.     }
  15. }
  16.  
  17. // ---------
  18.  
  19. #[derive(Debug)]
  20. struct A {
  21.   value: i32,    
  22. }
  23.  
  24. // Let's just implement OneAdder and get AnyAdder for free.
  25. impl OneAdder for A {
  26.     fn add_one(&mut self) {
  27.         self.value += 1;
  28.     }
  29. }
  30.  
  31. fn main() {
  32.     let mut a = A { value: 5 };
  33.     a.add_any(10);
  34.     println!("{:?}", a)
  35. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top