Advertisement
Guest User

Untitled

a guest
Dec 4th, 2016
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.06 KB | None | 0 0
  1. struct Usr {
  2. id: u32,
  3. health: u8,
  4. }
  5.  
  6. trait Player {
  7. fn get_id(&self) -> u32;
  8. fn get_health(&self) -> u8;
  9. fn set_health(&mut self, new_health: u8);
  10. }
  11.  
  12. impl Player for Usr {
  13. fn get_id(&self) -> u32 { self.id }
  14.  
  15. fn get_health(&self) -> u8 { self.health }
  16.  
  17. fn set_health(&mut self, new_health: u8) {
  18. self.health = new_health;
  19. }
  20. }
  21.  
  22. struct Admin {
  23. admin: bool,
  24. }
  25.  
  26. impl Player for Admin {
  27. fn get_id(&self) -> u32 { 0 }
  28.  
  29. fn get_health(&self) -> u8 { 100 }
  30.  
  31. #[allow(unused_variables)]
  32. fn set_health(&mut self, new_health: u8) {
  33. }
  34. }
  35.  
  36. fn health_test<T: Player>(player: &mut T) {
  37. println!("Player {} has health {}", player.get_id(), player.get_health());
  38. println!("Patch it health with: 80");
  39. player.set_health(80);
  40. println!("Now player {} has health {}", player.get_id(), player.get_health());
  41. }
  42.  
  43. fn main() {
  44. let mut user = Usr { id: 283745234, health: 100 };
  45. let mut admin = Admin { admin: true };
  46.  
  47. health_test(&user);
  48. health_test(&admin);
  49.  
  50. println!("Test user: health is {}", user.health);
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement