summaryrefslogtreecommitdiff
path: root/rustlings/exercises
diff options
context:
space:
mode:
Diffstat (limited to 'rustlings/exercises')
-rw-r--r--rustlings/exercises/07_structs/structs1.rs21
-rw-r--r--rustlings/exercises/07_structs/structs2.rs7
-rw-r--r--rustlings/exercises/07_structs/structs3.rs11
3 files changed, 21 insertions, 18 deletions
diff --git a/rustlings/exercises/07_structs/structs1.rs b/rustlings/exercises/07_structs/structs1.rs
index 959c4c6..8376a98 100644
--- a/rustlings/exercises/07_structs/structs1.rs
+++ b/rustlings/exercises/07_structs/structs1.rs
@@ -1,9 +1,10 @@
struct ColorRegularStruct {
- // TODO: Add the fields that the test `regular_structs` expects.
- // What types should the fields have? What are the minimum and maximum values for RGB colors?
+ red: u8,
+ green: u8,
+ blue: u8,
}
-struct ColorTupleStruct(/* TODO: Add the fields that the test `tuple_structs` expects */);
+struct ColorTupleStruct(u8, u8, u8);
#[derive(Debug)]
struct UnitStruct;
@@ -18,8 +19,11 @@ mod tests {
#[test]
fn regular_structs() {
- // TODO: Instantiate a regular struct.
- // let green =
+ let green = ColorRegularStruct {
+ red: 0,
+ green: 255,
+ blue: 0,
+ };
assert_eq!(green.red, 0);
assert_eq!(green.green, 255);
@@ -28,8 +32,7 @@ mod tests {
#[test]
fn tuple_structs() {
- // TODO: Instantiate a tuple struct.
- // let green =
+ let green = ColorTupleStruct(0, 255, 0 );
assert_eq!(green.0, 0);
assert_eq!(green.1, 255);
@@ -38,8 +41,8 @@ mod tests {
#[test]
fn unit_structs() {
- // TODO: Instantiate a unit struct.
- // let unit_struct =
+ let unit_struct = UnitStruct;
+
let message = format!("{unit_struct:?}s are fun!");
assert_eq!(message, "UnitStructs are fun!");
diff --git a/rustlings/exercises/07_structs/structs2.rs b/rustlings/exercises/07_structs/structs2.rs
index 79141af..2539e31 100644
--- a/rustlings/exercises/07_structs/structs2.rs
+++ b/rustlings/exercises/07_structs/structs2.rs
@@ -33,8 +33,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: String::from("Hacker in Rust"),
+ count: 1,
+ ..order_template
+ };
assert_eq!(your_order.name, "Hacker in Rust");
assert_eq!(your_order.year, order_template.year);
diff --git a/rustlings/exercises/07_structs/structs3.rs b/rustlings/exercises/07_structs/structs3.rs
index 69e5ced..d24da27 100644
--- a/rustlings/exercises/07_structs/structs3.rs
+++ b/rustlings/exercises/07_structs/structs3.rs
@@ -23,15 +23,12 @@ impl Package {
}
}
- // TODO: Add the correct return type to the function signature.
- fn is_international(&self) {
- // TODO: Read the tests that use this method to find out when a package
- // is considered international.
+ fn is_international(&self) -> bool {
+ self.sender_country != self.recipient_country
}
- // TODO: Add the correct return type to the function signature.
- fn get_fees(&self, cents_per_gram: u32) {
- // TODO: Calculate the package's fees.
+ fn get_fees(&self, cents_per_gram: u32) -> u32 {
+ self.weight_in_grams * cents_per_gram
}
}