daily pastebin goal
46%
SHARE
TWEET

Untitled

a guest Feb 18th, 2019 64 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. pub struct Cat{
  2.     color:(u8,u8,u8),
  3.    
  4. }
  5. pub struct Dog{
  6.     color:(u8,u8,u8),
  7. }
  8.  
  9. pub trait Speaker {
  10.      fn speak(&self) -> ();
  11.  }
  12.  
  13.  
  14.  
  15. impl Speaker for Cat {
  16.      fn speak(&self) {
  17.            println!("meow!");
  18.      }
  19.  }
  20.  
  21.  impl Speaker for Dog {
  22.      fn speak(&self) {
  23.            println!("woof!");
  24.      }
  25.  }
  26.  // fn speak<S: Speaker>() {
  27. //    self.speak();
  28. //}
  29.  
  30.  
  31. pub  fn notify<S: Speaker>(speaker: S) {
  32.         speaker.speak();
  33. }
  34.  
  35. fn main(){
  36.     let cat1 = Cat{color:(30,30,30)};
  37.     let dog1 = Dog{color:(30,30,30)};
  38.     cat1.speak();
  39.     dog1.speak();
  40.     notify(cat1);
  41. }
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
 
Top