Solve structs

This commit is contained in:
Manuel Gil 2020-03-01 09:29:29 +00:00
parent ce30485458
commit 99a3c200e0
2 changed files with 18 additions and 14 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 { struct ColorClassicStruct {
// TODO: Something goes here name: String,
hex: String,
} }
struct ColorTupleStruct(/* TODO: Something goes here */); struct ColorTupleStruct(String, String);
#[derive(Debug)] #[derive(Debug)]
struct UnitStruct; struct UnitStruct;
@ -18,8 +17,10 @@ mod tests {
#[test] #[test]
fn classic_c_structs() { fn classic_c_structs() {
// TODO: Instantiate a classic c struct! let green = ColorClassicStruct {
// let green = name: String::from("green"),
hex: String::from("#00FF00"),
};
assert_eq!(green.name, "green"); assert_eq!(green.name, "green");
assert_eq!(green.hex, "#00FF00"); assert_eq!(green.hex, "#00FF00");
@ -27,17 +28,14 @@ mod tests {
#[test] #[test]
fn tuple_structs() { fn tuple_structs() {
// TODO: Instantiate a tuple struct! let green = ColorTupleStruct(String::from("green"), String::from("#00FF00"));
// let green =
assert_eq!(green.0, "green"); assert_eq!(green.0, "green");
assert_eq!(green.1, "#00FF00"); assert_eq!(green.1, "#00FF00");
} }
#[test] #[test]
fn unit_structs() { fn unit_structs() {
// TODO: Instantiate a unit struct! let unit_struct = UnitStruct;
// let unit_struct =
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

@ -2,8 +2,6 @@
// Address all the TODOs to make the tests pass! // Address all the TODOs to make the tests pass!
// No hints, just do it! // No hints, just do it!
// I AM NOT DONE
#[derive(Debug)] #[derive(Debug)]
struct Order { struct Order {
name: String, name: String,
@ -35,7 +33,15 @@ 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: String::from("Hacker in Rust"),
year: 2019,
made_by_phone: false,
made_by_mobile: false,
made_by_email: true,
item_number: 123,
count: 1
};
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);