finish structs exercises

This commit is contained in:
lukaszKielar 2020-06-21 20:58:40 +02:00
parent f0ba1f7e92
commit c5a563d1d3
3 changed files with 28 additions and 24 deletions

View file

@ -1,13 +1,12 @@
// structs1.rs // structs1.rs
// Address all the TODOs to make the tests pass! // Address all the TODOs to make the tests pass!
// I AM NOT DONE struct ColorClassicStruct<'a> {
name: &'a str,
struct ColorClassicStruct { hex: &'a str,
// TODO: Something goes here
} }
struct ColorTupleStruct(/* TODO: Something goes here */); struct ColorTupleStruct<'a>(&'a str, &'a str);
#[derive(Debug)] #[derive(Debug)]
struct UnitStruct; struct UnitStruct;
@ -19,7 +18,10 @@ mod tests {
#[test] #[test]
fn classic_c_structs() { fn classic_c_structs() {
// TODO: Instantiate a classic c struct! // TODO: Instantiate a classic c struct!
// let green = let green = ColorClassicStruct {
name: "green",
hex: "#00FF00",
};
assert_eq!(green.name, "green"); assert_eq!(green.name, "green");
assert_eq!(green.hex, "#00FF00"); assert_eq!(green.hex, "#00FF00");
@ -28,7 +30,7 @@ mod tests {
#[test] #[test]
fn tuple_structs() { fn tuple_structs() {
// TODO: Instantiate a tuple struct! // TODO: Instantiate a tuple struct!
// let green = let green = ColorTupleStruct("green", "#00FF00");
assert_eq!(green.0, "green"); assert_eq!(green.0, "green");
assert_eq!(green.1, "#00FF00"); assert_eq!(green.1, "#00FF00");
@ -37,7 +39,7 @@ mod tests {
#[test] #[test]
fn unit_structs() { fn unit_structs() {
// TODO: Instantiate a unit struct! // TODO: Instantiate a unit struct!
// let unit_struct = let unit_struct = UnitStruct;
let message = format!("{:?}s are fun!", unit_struct); let message = format!("{:?}s are fun!", unit_struct);
assert_eq!(message, "UnitStructs are fun!"); assert_eq!(message, "UnitStructs are fun!");

View file

@ -1,8 +1,6 @@
// structs2.rs // structs2.rs
// Address all the TODOs to make the tests pass! // Address all the TODOs to make the tests pass!
// I AM NOT DONE
#[derive(Debug)] #[derive(Debug)]
struct Order { struct Order {
name: String, name: String,
@ -34,7 +32,11 @@ mod tests {
fn your_order() { fn your_order() {
let order_template = create_order_template(); let order_template = create_order_template();
// TODO: Create your own order using the update syntax and template above! // TODO: Create your own order using the update syntax and template above!
// let your_order = let your_order = Order {
name: "Hacker in Rust".to_string(),
count: 1,
..order_template
};
assert_eq!(your_order.name, "Hacker in Rust"); assert_eq!(your_order.name, "Hacker in Rust");
assert_eq!(your_order.year, order_template.year); assert_eq!(your_order.year, order_template.year);
assert_eq!(your_order.made_by_phone, order_template.made_by_phone); assert_eq!(your_order.made_by_phone, order_template.made_by_phone);

View file

@ -3,30 +3,33 @@
// exercise we have defined the Package struct and we want to test some logic attached to it, // exercise we have defined the Package struct and we want to test some logic attached to it,
// make the code compile and the tests pass! If you have issues execute `rustlings hint structs3` // make the code compile and the tests pass! If you have issues execute `rustlings hint structs3`
// I AM NOT DONE
#[derive(Debug)] #[derive(Debug)]
struct Package { struct Package {
from: String, from: String,
to: String, to: String,
weight: f32 weight: f32,
} }
impl Package { impl Package {
fn new(from: String, to: String, weight: f32) -> Package { fn new(from: String, to: String, weight: f32) -> Package {
if weight <= 0.0 { if weight <= 0.0 {
// Something goes here... let msg = format!("Weight must be greater than 0, {} specified", weight);
panic!(msg)
} else { } else {
return Package {from, to, weight}; return Package { from, to, weight };
} }
} }
fn is_international(&self) -> ??? { fn is_international(&self) -> bool {
// Something goes here... if self.from == self.to {
false
} else {
true
}
} }
fn get_fees(&self, cost_per_kg: f32) -> ??? { fn get_fees(&self, cost_per_kg: f32) -> f32 {
// Something goes here... self.weight * cost_per_kg
} }
} }
@ -47,7 +50,6 @@ mod tests {
fn create_international_package() { fn create_international_package() {
let country_from = String::from("Spain"); let country_from = String::from("Spain");
let country_to = String::from("Russia"); let country_to = String::from("Russia");
let package = Package::new(country_from, country_to, 1.2); let package = Package::new(country_from, country_to, 1.2);
assert!(package.is_international()); assert!(package.is_international());
@ -58,10 +60,8 @@ mod tests {
let country_from = String::from("Spain"); let country_from = String::from("Spain");
let country_to = String::from("Spain"); let country_to = String::from("Spain");
let country_fee = ???; let country_fee = 8.;
let package = Package::new(country_from, country_to, 22.0); let package = Package::new(country_from, country_to, 22.0);
assert_eq!(package.get_fees(country_fee), 176.0); assert_eq!(package.get_fees(country_fee), 176.0);
} }
} }