From 2ebbc987fb5df0c772b2734c37a13b62c209a3f0 Mon Sep 17 00:00:00 2001 From: rrokkam Date: Sun, 19 Jul 2020 13:18:50 -0700 Subject: [PATCH] Make Item::new take Into as a name This clears a lot of Rust-specific String boilerplate, so it's not necessary to type String::from("foo") every time we want an item with name "foo". It also makes the code look more similar to the C# version of the code. I am leaving the public struct members in because those are more similar to the matching code in the other languages' implementations. --- rust/src/gildedrose.rs | 6 +++--- rust/src/main.rs | 30 +++++++++--------------------- 2 files changed, 12 insertions(+), 24 deletions(-) diff --git a/rust/src/gildedrose.rs b/rust/src/gildedrose.rs index 876da88c..9e387756 100644 --- a/rust/src/gildedrose.rs +++ b/rust/src/gildedrose.rs @@ -6,9 +6,9 @@ pub struct Item { } impl Item { - pub fn new(name: String, sell_in: i32, quality: i32) -> Item { + pub fn new(name: impl Into, sell_in: i32, quality: i32) -> Item { Item { - name, + name: name.into(), sell_in, quality, } @@ -90,7 +90,7 @@ mod tests { #[test] pub fn foo() { - let items = vec![Item::new(String::from("foo"), 0, 0)]; + let items = vec![Item::new("foo", 0, 0)]; let mut rose = GildedRose::new(items); rose.update_quality(); diff --git a/rust/src/main.rs b/rust/src/main.rs index ceda22dd..eb973caf 100644 --- a/rust/src/main.rs +++ b/rust/src/main.rs @@ -4,28 +4,16 @@ use gildedrose::{GildedRose, Item}; fn main() { let items = vec![ - Item::new(String::from("+5 Dexterity Vest"), 10, 20), - Item::new(String::from("Aged Brie"), 2, 0), - Item::new(String::from("Elixir of the Mongoose"), 5, 7), - Item::new(String::from("Sulfuras, Hand of Ragnaros"), 0, 80), - Item::new(String::from("Sulfuras, Hand of Ragnaros"), -1, 80), - Item::new( - String::from("Backstage passes to a TAFKAL80ETC concert"), - 15, - 20, - ), - Item::new( - String::from("Backstage passes to a TAFKAL80ETC concert"), - 10, - 49, - ), - Item::new( - String::from("Backstage passes to a TAFKAL80ETC concert"), - 5, - 49, - ), + Item::new("+5 Dexterity Vest", 10, 20), + Item::new("Aged Brie", 2, 0), + Item::new("Elixir of the Mongoose", 5, 7), + Item::new("Sulfuras, Hand of Ragnaros", 0, 80), + Item::new("Sulfuras, Hand of Ragnaros", -1, 80), + Item::new("Backstage passes to a TAFKAL80ETC concert", 15, 20), + Item::new("Backstage passes to a TAFKAL80ETC concert", 10, 49), + Item::new("Backstage passes to a TAFKAL80ETC concert", 5, 49), // this conjured item does not work properly yet - Item::new(String::from("Conjured Mana Cake"), 3, 6), + Item::new("Conjured Mana Cake", 3, 6), ]; let mut rose = GildedRose::new(items);