complete: structs
This commit is contained in:
parent
3b960193a4
commit
c4a62515fb
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,9 +39,8 @@ 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,
|
||||
|
@ -14,6 +12,13 @@ struct Order {
|
|||
count: u32,
|
||||
}
|
||||
|
||||
impl Order {
|
||||
fn update<'a>(&mut self, value: &'a str){
|
||||
self.count += 1;
|
||||
self.name = String::from(value);
|
||||
}
|
||||
}
|
||||
|
||||
fn create_order_template() -> Order {
|
||||
Order {
|
||||
name: String::from("Bob"),
|
||||
|
@ -34,7 +39,8 @@ 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 mut your_order = create_order_template();
|
||||
your_order.update("Hacker in Rust");
|
||||
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);
|
||||
|
|
|
@ -4,8 +4,6 @@
|
|||
// Make the code compile and the tests pass!
|
||||
// If you have issues execute `rustlings hint structs3`
|
||||
|
||||
// I AM NOT DONE
|
||||
|
||||
#[derive(Debug)]
|
||||
struct Package {
|
||||
sender_country: String,
|
||||
|
@ -16,7 +14,7 @@ struct Package {
|
|||
impl Package {
|
||||
fn new(sender_country: String, recipient_country: String, weight_in_grams: i32) -> Package {
|
||||
if weight_in_grams <= 0 {
|
||||
// Something goes here...
|
||||
panic!("success!");
|
||||
} else {
|
||||
return Package {
|
||||
sender_country,
|
||||
|
@ -26,12 +24,12 @@ impl Package {
|
|||
}
|
||||
}
|
||||
|
||||
fn is_international(&self) -> ??? {
|
||||
// Something goes here...
|
||||
fn is_international(&self) -> bool {
|
||||
self.sender_country.as_str() != self.recipient_country.as_str()
|
||||
}
|
||||
|
||||
fn get_fees(&self, cents_per_gram: i32) -> ??? {
|
||||
// Something goes here...
|
||||
fn get_fees(&self, cents_per_gram: i32) -> i32 {
|
||||
self.weight_in_grams * cents_per_gram
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -73,7 +71,7 @@ mod tests {
|
|||
let sender_country = String::from("Spain");
|
||||
let recipient_country = String::from("Spain");
|
||||
|
||||
let cents_per_gram = ???;
|
||||
let cents_per_gram = 3;
|
||||
|
||||
let package = Package::new(sender_country, recipient_country, 1500);
|
||||
|
||||
|
|
Loading…
Reference in a new issue