Skip to content

Commit

Permalink
structs exercises
Browse files Browse the repository at this point in the history
  • Loading branch information
rganesan committed May 15, 2024
1 parent c40cb9c commit b40d053
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 20 deletions.
20 changes: 10 additions & 10 deletions exercises/07_structs/structs1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
// Execute `rustlings hint structs1` or use the `hint` watch subcommand for a
// hint.

// I AM NOT DONE

struct ColorClassicStruct {
// TODO: Something goes here
red: u8,
green: u8,
blue: u8
}

struct ColorTupleStruct(/* TODO: Something goes here */);
struct ColorTupleStruct(u8, u8, u8);

#[derive(Debug)]
struct UnitLikeStruct;
Expand All @@ -22,8 +22,8 @@ mod tests {

#[test]
fn classic_c_structs() {
// TODO: Instantiate a classic c struct!
// let green =
// Instantiate a classic c struct!
let green = ColorClassicStruct{red: 0, green: 255, blue: 0};

assert_eq!(green.red, 0);
assert_eq!(green.green, 255);
Expand All @@ -32,8 +32,8 @@ mod tests {

#[test]
fn tuple_structs() {
// TODO: Instantiate a tuple struct!
// let green =
// Instantiate a tuple struct!
let green = ColorTupleStruct(0, 255, 0);

assert_eq!(green.0, 0);
assert_eq!(green.1, 255);
Expand All @@ -42,8 +42,8 @@ mod tests {

#[test]
fn unit_structs() {
// TODO: Instantiate a unit-like struct!
// let unit_like_struct =
// Instantiate a unit-like struct!
let unit_like_struct = UnitLikeStruct{};
let message = format!("{:?}s are fun!", unit_like_struct);

assert_eq!(message, "UnitLikeStructs are fun!");
Expand Down
7 changes: 3 additions & 4 deletions exercises/07_structs/structs2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
// Execute `rustlings hint structs2` or use the `hint` watch subcommand for a
// hint.

// I AM NOT DONE

#[derive(Debug)]
struct Order {
name: String,
Expand Down Expand Up @@ -37,8 +35,9 @@ mod tests {
#[test]
fn your_order() {
let order_template = create_order_template();
// TODO: Create your own order using the update syntax and template above!
// let your_order =
// Create your own order using the update syntax and template above!
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);
Expand Down
11 changes: 5 additions & 6 deletions exercises/07_structs/structs3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
// Execute `rustlings hint structs3` or use the `hint` watch subcommand for a
// hint.

// I AM NOT DONE

#[derive(Debug)]
struct Package {
sender_country: String,
Expand All @@ -31,12 +29,13 @@ impl Package {
}
}

fn is_international(&self) -> ??? {
// Something goes here...
fn is_international(&self) -> bool {
self.sender_country != self.recipient_country
}

fn get_fees(&self, cents_per_gram: u32) -> ??? {
// Something goes here...
fn get_fees(&self, cents_per_gram: u32) -> u32 {
self.weight_in_grams * cents_per_gram

}
}

Expand Down

0 comments on commit b40d053

Please sign in to comment.