diff --git a/rust/tests/traits_test/Cargo.lock b/rust/tests/traits_test/Cargo.lock new file mode 100644 index 00000000..3e0e5745 --- /dev/null +++ b/rust/tests/traits_test/Cargo.lock @@ -0,0 +1,12 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "traits" +version = "0.1.0" + +[[package]] +name = "traits_test" +version = "0.1.0" +dependencies = [ + "traits", +] diff --git a/rust/tests/traits_test/Cargo.toml b/rust/tests/traits_test/Cargo.toml new file mode 100644 index 00000000..690edcb6 --- /dev/null +++ b/rust/tests/traits_test/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "traits_test" +version = "0.1.0" +authors = ["Augusto "] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +traits = { path = "../../../../rust-piscine-solutions/traits"} \ No newline at end of file diff --git a/rust/tests/traits_test/src/main.rs b/rust/tests/traits_test/src/main.rs new file mode 100644 index 00000000..8eee2da9 --- /dev/null +++ b/rust/tests/traits_test/src/main.rs @@ -0,0 +1,67 @@ +// Imagine you are designing a new video game and you have to create +// food that they players can take to gain strength there are two +// types of food for now fruits and meet: fruits increases the +// strengths by 1 unit and meat increases it by 3 unit. + +// Define both structures fruits and meat +// Define the std::fmt::Display trait of the Player structure so using +// the template {} inside a println! macro will print in the first +// line the name of the player +// in the second line the strength, score and the money +// and in the third line the weapons +use traits::{Food, Fruit, Meat, Player}; + +fn main() { + let apple = Fruit { weight_in_kg: 1.0 }; + assert_eq!(apple.gives(), 4); + let steak = Meat { + weight_in_kg: 1.0, + fat_content: 1.0, + }; + + let mut player1 = Player { + name: String::from("player1"), + strength: 1, + score: 0, + money: 0, + weapons: vec![String::from("knife")], + }; + println!("Before eating {:?}", player1); + player1.eat(apple); + println!("After eating an apple\n{:?}", player1); + player1.eat(steak); + println!("After eating a steak\n{:?}", player1); +} + +#[test] +fn test_gives() { + let apple = Fruit { weight_in_kg: 1.0 }; + assert_eq!(apple.gives(), 4); + let steak = Meat { + weight_in_kg: 1.0, + fat_content: 1.0, + }; + assert_eq!(steak.gives(), 9); +} + +#[test] +fn test_eat() { + let apple = Fruit { weight_in_kg: 1.0 }; + assert_eq!(apple.gives(), 4); + let steak = Meat { + weight_in_kg: 1.0, + fat_content: 1.0, + }; + + let mut player1 = Player { + name: String::from("player1"), + strength: 1, + score: 0, + money: 0, + weapons: vec![String::from("knife")], + }; + player1.eat(apple); + assert_eq!(player1.strength, 5); + player1.eat(steak); + assert_eq!(player1.strength, 14); +} diff --git a/subjects/traits/README.md b/subjects/traits/README.md new file mode 100644 index 00000000..05157ebf --- /dev/null +++ b/subjects/traits/README.md @@ -0,0 +1,93 @@ +## traits + +### Instructions + +Imagine you are designing a new video game and you have to create food that they players can take to gain strength. + +There are two types of food for now fruits and meet: fruits increases the strengths by 1 unit and meat increases it by 3 unit. + +- Define both structures fruits and meat: + +Define the std::fmt::Display trait of the Player structure so using the template {} inside a println! macro will print: + +- In the first line the name of the player +- In the second line the strength, score and the money +- In the third line the weapons + +### Expected Functions and Structures + +```rust +#[derive(Debug)] +pub struct Player { + pub name: String, + pub strength: u32, + pub score: i32, + pub money: i32, + pub weapons: Vec, +} + +pub struct Fruit { + pub weight_in_kg: f64, +} + +pub struct Meat { + pub weight_in_kg: f64, + pub fat_content: f64, +} + +impl Player { + fn eat(&mut self, food: T) { + self.strength += food.gives(); + } +} + +pub trait Food { + fn gives(&self) -> u32; +} + +impl Food for Fruit { +} + +impl Food for Meat { +} +``` + +### Usage + +Here is a program to test your function. + +```rust +fn main() { + let apple = Fruit { weight_in_kg: 1.0 }; + assert_eq!(apple.gives(), 4); + let steak = Meat { + weight_in_kg: 1.0, + fat_content: 1.0, + }; + + let mut player1 = Player { + name: String::from("player1"), + strength: 1, + score: 0, + money: 0, + weapons: vec![String::from("knife")], + }; + println!("Before eating {:?}", player1); + player1.eat(apple); + println!("After eating an apple\n{:?}", player1); + player1.eat(steak); + 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"] } +After eating an apple +Player { name: "player1", strength: 5, score: 0, money: 0, weapons: ["knife"] } +After eating a steak +Player { name: "player1", strength: 14, score: 0, money: 0, weapons: ["knife"] } +student@ubuntu:~/[[ROOT]]/test$ +```