From 6ea638aad7c5cea14b330e1a9b371ecec1349c7d Mon Sep 17 00:00:00 2001 From: Augusto Date: Fri, 19 Mar 2021 23:12:33 +0000 Subject: [PATCH] Clarify instructions for exercise `traits` --- subjects/traits/README.md | 38 +++++++++++++++++++++++++------------- 1 file changed, 25 insertions(+), 13 deletions(-) diff --git a/subjects/traits/README.md b/subjects/traits/README.md index 69c2ac21..eff32305 100644 --- a/subjects/traits/README.md +++ b/subjects/traits/README.md @@ -4,11 +4,14 @@ Imagine you are designing a new video game and you have to create food that the players can eat to gain strength. -There are two types of food for now, fruits and meat: fruits increase the strength by 1 unit and meat increases it by 3 units. +There are two types of food for now: -- Define both structures fruits and meat: +- Fruit: increase the strength by 4 unit per each kilogram of fruit consumed. +- Meat: has the weight in kilograms -> `weight_in_kg` (which is the weight of the whole piece) and the fat_content which corresponds to the percentage of the weight which is pure fat (the rest is consider protein) each kilogram of protein gives 4 units of `strenght` and each kilogram of fat gives 9 units of `strength`. -Define the std::fmt::Display trait of the Player structure in a way that when using the template {} inside a println! macro it will print: +Define the `Food` trait for `Fruit` and `Meat`. The method require method `gives()` represents the energy that the food provides. + +Implement the `std::fmt::Display` trait for `Player` structure in a way that when using the template `{}` inside a println! macro it will print: - In the first line, the name of the player - In the second line the strength, score and the money @@ -20,7 +23,7 @@ Define the std::fmt::Display trait of the Player structure in a way that when us #[derive(Debug)] pub struct Player { pub name: String, - pub strength: u32, + pub strength: f64, pub score: i32, pub money: i32, pub weapons: Vec, @@ -54,14 +57,17 @@ impl Food for Meat { ### Usage -Here is a program to test your function. +Here is a program to test your functions and traits. ```rust -use generics::*; +use traits::*; + fn main() { let apple = Fruit { weight_in_kg: 1.0 }; - assert_eq!(apple.gives(), 4); + + println!("this apple gives {} units of strength", apple.gives()); + let steak = Meat { weight_in_kg: 1.0, fat_content: 1.0, @@ -69,27 +75,33 @@ fn main() { let mut player1 = Player { name: String::from("player1"), - strength: 1, + strength: 1.0, score: 0, money: 0, weapons: vec![String::from("knife")], }; println!("Before eating {:?}", player1); player1.eat(apple); - println!("After eating an apple\n{:?}", player1); + println!("After eating an apple\n{}", player1); player1.eat(steak); - println!("After eating a steak\n{:?}", player1); + println!("After eating a steak\n{}", player1); } + ``` And its output: ```console student@ubuntu:~/[[ROOT]]/test$ cargo run -Before eating Player { name: "player1", strength: 1, score: 0, money: 0, weapons: ["knife"] } +this apple gives 4 units of strength +Before eating Player { name: "player1", strength: 1.0, score: 0, money: 0, weapons: ["knife"] } After eating an apple -Player { name: "player1", strength: 5, score: 0, money: 0, weapons: ["knife"] } +player1 +Strength: 5, Score: 0, Money: 0 +Weapons: ["knife"] After eating a steak -Player { name: "player1", strength: 14, score: 0, money: 0, weapons: ["knife"] } +player1 +Strength: 14, Score: 0, Money: 0 +Weapons: ["knife"] student@ubuntu:~/[[ROOT]]/test$ ```