SHARE
TWEET

Untitled

a guest Apr 20th, 2019 71 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. struct Weapon {
  2.     damage: u8,
  3. }
  4.  
  5. struct Armor {
  6.     defense: u8,
  7. }
  8.  
  9. enum Item {
  10.    Weap(Weapon),
  11.    Arm(Armor),
  12. }
  13.  
  14. impl Item {
  15.     fn damage(&self) -> Option<u8> {
  16.         match self {
  17.             Item::Weap(weapon) => Some(weapon.damage),
  18.             _ => None,
  19.         }
  20.     }
  21.  
  22.     fn defense(&self) -> Option<u8> {
  23.         match self {
  24.             Item::Arm(armor) => Some(armor.defense),
  25.             _ => None,
  26.         }
  27.     }
  28. }
  29.  
  30. fn main() {
  31.     let item_weapon = Item::Weap(Weapon { damage: 15 });
  32.     println!("weapon.damage: {:?}", item_weapon.damage());
  33.     println!("weapon.defense: {:?}", item_weapon.defense());
  34.  
  35.     let item_armor = Item::Arm(Armor { defense: 15 });
  36.     println!("armor.damage: {:?}", item_armor.damage());
  37.     println!("armor.defense: {:?}", item_armor.defense());
  38. }
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