Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- struct Usr {
- id: u32,
- health: u8,
- }
- trait Player {
- fn get_id(&self) -> u32;
- fn get_health(&self) -> u8;
- fn set_health(&mut self, new_health: u8);
- }
- impl Player for Usr {
- fn get_id(&self) -> u32 { self.id }
- fn get_health(&self) -> u8 { self.health }
- fn set_health(&mut self, new_health: u8) {
- self.health = new_health;
- }
- }
- struct Admin {
- admin: bool,
- }
- impl Player for Admin {
- fn get_id(&self) -> u32 { 0 }
- fn get_health(&self) -> u8 { 100 }
- #[allow(unused_variables)]
- fn set_health(&mut self, new_health: u8) {
- }
- }
- fn health_test<T: Player>(player: &mut T) {
- println!("Player {} has health {}", player.get_id(), player.get_health());
- println!("Patch it health with: 80");
- player.set_health(80);
- println!("Now player {} has health {}", player.get_id(), player.get_health());
- }
- fn main() {
- let mut user = Usr { id: 283745234, health: 100 };
- let mut admin = Admin { admin: true };
- health_test(&user);
- health_test(&admin);
- println!("Test user: health is {}", user.health);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement