enums and struct

This commit is contained in:
jiangfangping 2024-07-09 11:44:52 +08:00
parent 17f5f39e51
commit f05c85be0d
6 changed files with 50 additions and 8 deletions

View File

@ -1,9 +1,12 @@
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: i32,
green: i32,
blue: i32,
}
struct ColorTupleStruct(/* TODO: Add the fields that the test `tuple_structs` expects */);
struct ColorTupleStruct(i32, i32, i32);
#[derive(Debug)]
struct UnitStruct;
@ -19,7 +22,7 @@ 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);
@ -29,7 +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);
@ -39,7 +42,7 @@ 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!");

View File

@ -34,7 +34,15 @@ mod tests {
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);

View File

@ -24,14 +24,16 @@ impl Package {
}
// TODO: Add the correct return type to the function signature.
fn is_international(&self) {
fn is_international(&self) -> bool {
// TODO: Read the tests that use this method to find out when a package
// is considered international.
self.sender_country != self.recipient_country
}
// TODO: Add the correct return type to the function signature.
fn get_fees(&self, cents_per_gram: u32) {
fn get_fees(&self, cents_per_gram: u32) -> u32 {
// TODO: Calculate the package's fees.
cents_per_gram * self.weight_in_grams
}
}

View File

@ -1,6 +1,11 @@
#[derive(Debug)]
enum Message {
// TODO: Define a few types of messages as used below.
Resize,
Move,
Echo,
ChangeColor,
Quit,
}
fn main() {

View File

@ -9,6 +9,14 @@ struct Point {
#[derive(Debug)]
enum Message {
// TODO: Define the different variants used below.
Resize {
width: i32,
height: i32,
},
Move(Point),
Echo(String),
ChangeColor(i32, i32, i32),
Quit
}
impl Message {

View File

@ -4,7 +4,14 @@ struct Point {
}
enum Message {
// TODO: Implement the message variant types based on their usage below.
Resize {
width: u64,
height: u64,
},
Move(Point),
Echo(String),
ChangeColor(u8, u8, u8),
Quit,
}
struct State {
@ -42,6 +49,15 @@ impl State {
fn process(&mut self, message: Message) {
// TODO: Create a match expression to process the different message
// variants using the methods defined above.
match message {
Message::Resize { width, height } => {
self.resize(width, height)
}
Message::Move(point) => { self.move_position(point) }
Message::Echo(msg) => { self.echo(msg) }
Message::ChangeColor(red, green, blue) => { self.change_color(red, green, blue) }
Message::Quit => { self.quit() }
}
}
}