mirror of
https://github.com/emilybache/GildedRose-Refactoring-Kata.git
synced 2026-10-13 17:49:25 +00:00
Implement Display for Item
This is a more idiomatic way of printing the contents of an object than reaching into its innards. It is also more 1-1 with the original C# code, which overrides toString for Item.
This commit is contained in:
1 parent
f21ed2ae13
commit
c88bdfd53e
2 files changed
+8
-1
No files matched your search
@@ -1,3 +1,4 @@
|
||||
use std::fmt::{self, Display};
|
||||
pub struct Item {
|
||||
pub name: String,
|
||||
pub sell_in: i32,
|
||||
@@ -14,6 +15,12 @@ impl Item {
|
||||
}
|
||||
}
|
||||
|
||||
impl Display for Item {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
write!(f, "{}, {}, {}", self.name, self.sell_in, self.quality)
|
||||
}
|
||||
}
|
||||
|
||||
pub struct GildedRose {
|
||||
pub items: Vec<Item>,
|
||||
}
|
||||
|
||||
+1
-1
@@ -34,7 +34,7 @@ fn main() {
|
||||
println!("-------- day {} --------", i);
|
||||
println!("name, sellIn, quality");
|
||||
for item in &rose.items {
|
||||
println!("{}, {}, {}", item.name, item.sell_in, item.quality);
|
||||
println!("{}", item);
|
||||
}
|
||||
println!();
|
||||
rose.update_quality();
|
||||
|
||||
Reference in new issue
Block a user