From 418f514fc46f45ae2901753e3398adb33664bed9 Mon Sep 17 00:00:00 2001 From: Andreas Grois Date: Sun, 2 Apr 2023 21:38:17 +0200 Subject: I, for one, welcome our new clippy overlords --- examples/text-adventure/data.rs | 25 ++++++------------------- 1 file changed, 6 insertions(+), 19 deletions(-) (limited to 'examples/text-adventure/data.rs') diff --git a/examples/text-adventure/data.rs b/examples/text-adventure/data.rs index 2c12bb0..3e9c49e 100644 --- a/examples/text-adventure/data.rs +++ b/examples/text-adventure/data.rs @@ -83,19 +83,13 @@ pub enum Item{ impl Item { fn price(self) -> usize { match self { - Item::SausageRoll => 300, - Item::Pickles => 250, - Item::Milk => 125, - Item::Yoghurt => 125, + Item::SausageRoll | Item::FishSandwich | Item::Shots => 300, + Item::Pickles | Item::Pulp => 250, + Item::Milk | Item::Yoghurt | Item::Beer => 125, Item::Cheese => 750, Item::CatFood => 2500, - Item::Beer => 125, Item::ToiletPaper => 500, - Item::FishSandwich => 300, Item::ChewingGum => 100, - Item::Shots => 300, - Item::Pulp => 250, - } } pub fn description(self) -> &'static str { @@ -112,7 +106,6 @@ impl Item { Item::ChewingGum => "A pack of chewing gum, costing €1.00", Item::Shots => "A shot of a sad excuse for whisky, costing €3.00", Item::Pulp => "A pulp novel called \"Aliens ate my trashbin\", which should not cost the €2.50 it does", - } } } @@ -131,17 +124,11 @@ impl Location{ //In a real project I would probably aim to make this Copy as well, especially if it's as small as this. //I left it as Clone intentionally, to illustrate how one can work around the limitation of it not being Copy. -#[derive(Clone)] +#[derive(Clone, Default)] pub struct Inventory { pub items : Vec, } -impl Default for Inventory{ - fn default() -> Self { - Self { items: Default::default() } - } -} - impl Inventory{ pub fn has_item_from_room(&self, room : Location) -> bool { let items_from_room = room.items(); @@ -151,7 +138,7 @@ impl Inventory{ if self.items.len() < 3 { let mut items = self.items; items.push(item); //am I the only one that hates that push doesn't return the updated vec? - Ok(Inventory{items, ..self}) + Ok(Inventory{items}) } else { Err(self) } @@ -170,7 +157,7 @@ impl Inventory{ "€10.00" //It doesn't change. Can as well be a constant. } pub fn total_price(&self) -> usize { - self.items.iter().cloned().map(Item::price).sum::() + self.items.iter().copied().map(Item::price).sum::() } pub fn can_afford(&self) -> bool { self.total_price() <= 1000_usize -- cgit v1.2.3