finish structs exercises
This commit is contained in:
parent
f0ba1f7e92
commit
c5a563d1d3
exercises/structs
|
@ -1,13 +1,12 @@
|
|||
// structs1.rs
|
||||
// Address all the TODOs to make the tests pass!
|
||||
|
||||
// I AM NOT DONE
|
||||
|
||||
struct ColorClassicStruct {
|
||||
// TODO: Something goes here
|
||||
struct ColorClassicStruct<'a> {
|
||||
name: &'a str,
|
||||
hex: &'a str,
|
||||
}
|
||||
|
||||
struct ColorTupleStruct(/* TODO: Something goes here */);
|
||||
struct ColorTupleStruct<'a>(&'a str, &'a str);
|
||||
|
||||
#[derive(Debug)]
|
||||
struct UnitStruct;
|
||||
|
@ -19,7 +18,10 @@ mod tests {
|
|||
#[test]
|
||||
fn classic_c_structs() {
|
||||
// TODO: Instantiate a classic c struct!
|
||||
// let green =
|
||||
let green = ColorClassicStruct {
|
||||
name: "green",
|
||||
hex: "#00FF00",
|
||||
};
|
||||
|
||||
assert_eq!(green.name, "green");
|
||||
assert_eq!(green.hex, "#00FF00");
|
||||
|
@ -28,7 +30,7 @@ mod tests {
|
|||
#[test]
|
||||
fn tuple_structs() {
|
||||
// TODO: Instantiate a tuple struct!
|
||||
// let green =
|
||||
let green = ColorTupleStruct("green", "#00FF00");
|
||||
|
||||
assert_eq!(green.0, "green");
|
||||
assert_eq!(green.1, "#00FF00");
|
||||
|
@ -37,7 +39,7 @@ mod tests {
|
|||
#[test]
|
||||
fn unit_structs() {
|
||||
// TODO: Instantiate a unit struct!
|
||||
// let unit_struct =
|
||||
let unit_struct = UnitStruct;
|
||||
let message = format!("{:?}s are fun!", unit_struct);
|
||||
|
||||
assert_eq!(message, "UnitStructs are fun!");
|
||||
|
|
|
@ -1,8 +1,6 @@
|
|||
// structs2.rs
|
||||
// Address all the TODOs to make the tests pass!
|
||||
|
||||
// I AM NOT DONE
|
||||
|
||||
#[derive(Debug)]
|
||||
struct Order {
|
||||
name: String,
|
||||
|
@ -34,7 +32,11 @@ mod tests {
|
|||
fn your_order() {
|
||||
let order_template = create_order_template();
|
||||
// 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.year, order_template.year);
|
||||
assert_eq!(your_order.made_by_phone, order_template.made_by_phone);
|
||||
|
|
|
@ -3,30 +3,33 @@
|
|||
// 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`
|
||||
|
||||
// I AM NOT DONE
|
||||
|
||||
#[derive(Debug)]
|
||||
struct Package {
|
||||
from: String,
|
||||
to: String,
|
||||
weight: f32
|
||||
weight: f32,
|
||||
}
|
||||
|
||||
impl Package {
|
||||
fn new(from: String, to: String, weight: f32) -> Package {
|
||||
if weight <= 0.0 {
|
||||
// Something goes here...
|
||||
let msg = format!("Weight must be greater than 0, {} specified", weight);
|
||||
panic!(msg)
|
||||
} else {
|
||||
return Package {from, to, weight};
|
||||
return Package { from, to, weight };
|
||||
}
|
||||
}
|
||||
|
||||
fn is_international(&self) -> ??? {
|
||||
// Something goes here...
|
||||
fn is_international(&self) -> bool {
|
||||
if self.from == self.to {
|
||||
false
|
||||
} else {
|
||||
true
|
||||
}
|
||||
}
|
||||
|
||||
fn get_fees(&self, cost_per_kg: f32) -> ??? {
|
||||
// Something goes here...
|
||||
fn get_fees(&self, cost_per_kg: f32) -> f32 {
|
||||
self.weight * cost_per_kg
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -47,7 +50,6 @@ mod tests {
|
|||
fn create_international_package() {
|
||||
let country_from = String::from("Spain");
|
||||
let country_to = String::from("Russia");
|
||||
|
||||
let package = Package::new(country_from, country_to, 1.2);
|
||||
|
||||
assert!(package.is_international());
|
||||
|
@ -58,10 +60,8 @@ mod tests {
|
|||
let country_from = 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);
|
||||
|
||||
assert_eq!(package.get_fees(country_fee), 176.0);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue