Add solution to structs2.rs
This commit is contained in:
parent
edec52eeff
commit
301e378b4d
|
@ -1,7 +1,7 @@
|
||||||
// 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
|
// Not Done
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
struct Order {
|
struct Order {
|
||||||
|
@ -26,6 +26,12 @@ fn create_order_template() -> Order {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// let your_order = Order{
|
||||||
|
// name: String::from("Hacker in Rust"),
|
||||||
|
// count: 1,
|
||||||
|
// ..order_template //dot dot operator meaning the rest of struct fields remains the same
|
||||||
|
// };
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
@ -34,20 +40,21 @@ 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;
|
|
||||||
|
|
||||||
// added the ampersand after the variable let_order to solve the compiler error msg below
|
// this struct is moved from above to inside this function.
|
||||||
// move occurs because `order_template` has type `Order`, which does not implement the `Copy` trait
|
// create own struct version changing the name and count while the remaining fields remain the same
|
||||||
let your_order = &order_template;
|
let your_order = Order{
|
||||||
//assert_eq!(your_order.name, "Hacker in Rust");
|
name: String::from("Hacker in Rust"),
|
||||||
assert_eq!(your_order.name, "Bob");
|
count: 1,
|
||||||
|
..order_template //dot dot operator meaning the rest of struct fields remain the same
|
||||||
|
};
|
||||||
|
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);
|
||||||
assert_eq!(your_order.made_by_mobile, order_template.made_by_mobile);
|
assert_eq!(your_order.made_by_mobile, order_template.made_by_mobile);
|
||||||
assert_eq!(your_order.made_by_email, order_template.made_by_email);
|
assert_eq!(your_order.made_by_email, order_template.made_by_email);
|
||||||
assert_eq!(your_order.item_number, order_template.item_number);
|
assert_eq!(your_order.item_number, order_template.item_number);
|
||||||
//assert_eq!(your_order.count, 1);
|
assert_eq!(your_order.count, 1);
|
||||||
assert_eq!(your_order.count, 0);
|
//assert_eq!(your_order.count, 0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue