SHARE
TWEET

Untitled

a guest Jun 24th, 2019 52 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. struct Dog {
  2.     name: &'static str
  3. }
  4.  
  5. trait Animal : Noisy {
  6.     fn Run(&self);
  7. }
  8.  
  9. trait Noisy {
  10.     fn Noise(&self) -> String;
  11. }
  12.  
  13. trait Threat : Animal {
  14.     fn EatHuman(&self) ->String;
  15. }
  16.  
  17. impl Animal for Dog {
  18.     fn Run(&self) { println!("On all fours, and wagging tail.")}
  19. }
  20.  
  21. struct WildDog {
  22.     dog:Dog
  23. }
  24.  
  25. impl Animal for WildDog
  26. {
  27.     fn Run(&self) { println!("On all fours, and nose low.")}
  28. }
  29.  
  30. impl Noisy for Dog {
  31.     fn Noise(&self) -> String {
  32.         return String::from("Bark!");
  33.     }
  34. }
  35.  
  36. impl Noisy for WildDog{
  37.     fn Noise(&self) -> String {
  38.         return String::from("Yelp!");
  39.     }
  40. }
  41.  
  42. impl Threat for WildDog
  43. {
  44.     fn EatHuman(&self) -> String{
  45.        return String::from("On all fours, and nose low.");
  46.     }
  47. }
  48.  
  49. fn main() {
  50.     // Type annotation is necessary in this case.
  51.     let dolly = Dog{name: "Dolly"};
  52.     // TODO ^ Try removing the type annotations.
  53.  
  54.     GoToThePark(&dolly);
  55.     MeTryingToSleep(&dolly);
  56.  
  57.     let c = WildDog{ dog: Dog{name: "Stephan"}};
  58.    
  59.     GoToThePark(&c);
  60.     MeTryingToSleep(&c);
  61.     BeScared(&c);
  62. }
  63.  
  64. fn GoToThePark<T: Animal>(x: &T) {
  65.     println!("++++++++\nMe going to the park\n++++++++\n");
  66.     x.Run();
  67.     println!("{}",x.Noise())
  68. }
  69.  
  70. fn MeTryingToSleep<T: Noisy>(x : &T) {
  71.     println!("++++++++\nMe trying to sleep\n++++++++\n");
  72.     println!("{}",x.Noise())
  73. }
  74.  
  75. fn BeScared<T :Threat>(x : &T) {
  76.     println!("++++++++\nMe being scared\n++++++++\n");
  77.     GoToThePark(x);
  78.     println!("{}",x.EatHuman())
  79. }
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