Make Item::new take Into<String> 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.
This commit is contained in:
rrokkam 2020-07-19 13:18:50 -07:00
parent c88bdfd53e
commit 2ebbc987fb
2 changed files with 12 additions and 24 deletions

View File

@ -6,9 +6,9 @@ pub struct Item {
} }
impl Item { impl Item {
pub fn new(name: String, sell_in: i32, quality: i32) -> Item { pub fn new(name: impl Into<String>, sell_in: i32, quality: i32) -> Item {
Item { Item {
name, name: name.into(),
sell_in, sell_in,
quality, quality,
} }
@ -90,7 +90,7 @@ mod tests {
#[test] #[test]
pub fn foo() { 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); let mut rose = GildedRose::new(items);
rose.update_quality(); rose.update_quality();

View File

@ -4,28 +4,16 @@ use gildedrose::{GildedRose, Item};
fn main() { fn main() {
let items = vec![ let items = vec![
Item::new(String::from("+5 Dexterity Vest"), 10, 20), Item::new("+5 Dexterity Vest", 10, 20),
Item::new(String::from("Aged Brie"), 2, 0), Item::new("Aged Brie", 2, 0),
Item::new(String::from("Elixir of the Mongoose"), 5, 7), Item::new("Elixir of the Mongoose", 5, 7),
Item::new(String::from("Sulfuras, Hand of Ragnaros"), 0, 80), Item::new("Sulfuras, Hand of Ragnaros", 0, 80),
Item::new(String::from("Sulfuras, Hand of Ragnaros"), -1, 80), Item::new("Sulfuras, Hand of Ragnaros", -1, 80),
Item::new( Item::new("Backstage passes to a TAFKAL80ETC concert", 15, 20),
String::from("Backstage passes to a TAFKAL80ETC concert"), Item::new("Backstage passes to a TAFKAL80ETC concert", 10, 49),
15, Item::new("Backstage passes to a TAFKAL80ETC concert", 5, 49),
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,
),
// this conjured item does not work properly yet // 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); let mut rose = GildedRose::new(items);