Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
saiyuan233 committed Dec 24, 2024
1 parent 752ddcb commit 9e0cd28
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 8 deletions.
4 changes: 4 additions & 0 deletions exercises/enums/enums1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
#[derive(Debug)]
enum Message {
// TODO: define a few types of messages as used below
Quit,
Echo,
Move,
ChangeColor
}

fn main() {
Expand Down
4 changes: 4 additions & 0 deletions exercises/enums/enums2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
#[derive(Debug)]
enum Message {
// TODO: define the different variants used below
Move { x: i32, y: i32 },
Echo(String),
ChangeColor(i32, i32, i32),
Quit
}

impl Message {
Expand Down
10 changes: 10 additions & 0 deletions exercises/enums/enums3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@

enum Message {
// TODO: implement the message variant types based on their usage below
ChangeColor(u8, u8, u8),
Echo(String),
Move(Point),
Quit
}

struct Point {
Expand Down Expand Up @@ -43,6 +47,12 @@ impl State {
// variants
// Remember: When passing a tuple as a function argument, you'll need
// extra parentheses: fn function((t, u, p, l, e))
match message {
Message::ChangeColor(red, green, blue) => self.change_color((red, green, blue)),
Message::Echo(msg) => self.echo(msg),
Message::Move(point) => self.move_position(point),
Message::Quit => self.quit()
}
}
}

Expand Down
16 changes: 11 additions & 5 deletions exercises/structs/structs1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,12 @@

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

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

#[derive(Debug)]
struct UnitLikeStruct;
Expand All @@ -23,8 +26,11 @@ mod tests {
#[test]
fn classic_c_structs() {
// TODO: Instantiate a classic c struct!
// let green =

let green = ColorClassicStruct {
red: 0,
green: 255,
blue: 0
};
assert_eq!(green.red, 0);
assert_eq!(green.green, 255);
assert_eq!(green.blue, 0);
Expand All @@ -33,7 +39,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);
Expand All @@ -43,7 +49,7 @@ mod tests {
#[test]
fn unit_structs() {
// TODO: Instantiate a unit-like struct!
// let 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
10 changes: 9 additions & 1 deletion exercises/structs/structs2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,15 @@ 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"),
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.year, order_template.year);
assert_eq!(your_order.made_by_phone, order_template.made_by_phone);
Expand Down
6 changes: 4 additions & 2 deletions exercises/structs/structs3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,14 @@ impl Package {
}
}

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

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

Expand Down

0 comments on commit 9e0cd28

Please sign in to comment.