structs and enums done

This commit is contained in:
Chris Girvin 2022-05-16 20:26:18 -04:00
parent 884a0b6a51
commit d961e4695b
6 changed files with 65 additions and 22 deletions

View File

@ -1,11 +1,13 @@
// enums1.rs // enums1.rs
// Make me compile! Execute `rustlings hint enums1` for hints! // Make me compile! Execute `rustlings hint enums1` for hints!
// I AM NOT DONE
#[derive(Debug)] #[derive(Debug)]
enum Message { enum Message {
// TODO: define a few types of messages as used below // TODO: define a few types of messages as used below
Quit,
Echo,
Move,
ChangeColor,
} }
fn main() { fn main() {

View File

@ -1,11 +1,13 @@
// enums2.rs // enums2.rs
// Make me compile! Execute `rustlings hint enums2` for hints! // Make me compile! Execute `rustlings hint enums2` for hints!
// I AM NOT DONE
#[derive(Debug)] #[derive(Debug)]
enum Message { enum Message {
// TODO: define the different variants used below // TODO: define the different variants used below
Move { x: i32, y: i32 },
Echo(String),
ChangeColor(u8, u8, u8),
Quit,
} }
impl Message { impl Message {

View File

@ -1,10 +1,18 @@
// enums3.rs // enums3.rs
// Address all the TODOs to make the tests pass! // Address all the TODOs to make the tests pass!
// I AM NOT DONE
enum Message { enum Message {
// TODO: implement the message variant types based on their usage below // TODO: implement the message variant types based on their usage below
// // solution0
// ChangeColor((u8, u8, u8)),
// Echo(String),
// Move(Point),
// Quit,
// solution1
ChangeColor { color: (u8, u8, u8) },
Echo { s: String },
Move { p: Point },
Quit,
} }
struct Point { struct Point {
@ -37,6 +45,18 @@ impl State {
fn process(&mut self, message: Message) { fn process(&mut self, message: Message) {
// TODO: create a match expression to process the different message variants // TODO: create a match expression to process the different message variants
match message {
// // solution0
// Message::ChangeColor((r, g, b)) => self.change_color((r, g, b)),
// Message::Echo(s) => self.echo(s),
// Message::Move(p) => self.move_position(p),
// Message::Quit => self.quit(),
// solution1
Message::ChangeColor { color } => self.change_color(color),
Message::Echo { s } => self.echo(s),
Message::Move { p } => self.move_position(p),
Message::Quit => self.quit(),
}
} }
} }
@ -51,9 +71,21 @@ mod tests {
position: Point { x: 0, y: 0 }, position: Point { x: 0, y: 0 },
color: (0, 0, 0), color: (0, 0, 0),
}; };
state.process(Message::ChangeColor((255, 0, 255))); // // solution0
state.process(Message::Echo(String::from("hello world"))); // state.process(Message::ChangeColor((255, 0, 255)));
state.process(Message::Move(Point { x: 10, y: 15 })); // state.process(Message::Echo(String::from("hello world")));
// state.process(Message::Move(Point { x: 10, y: 15 }));
// state.process(Message::Quit);
// solution1
state.process(Message::ChangeColor {
color: (255, 0, 255),
});
state.process(Message::Echo {
s: String::from("hello world"),
});
state.process(Message::Move {
p: Point { x: 10, y: 15 },
});
state.process(Message::Quit); state.process(Message::Quit);
assert_eq!(state.color, (255, 0, 255)); assert_eq!(state.color, (255, 0, 255));

View File

@ -1,13 +1,13 @@
// structs1.rs // structs1.rs
// Address all the TODOs to make the tests pass! // Address all the TODOs to make the tests pass!
// I AM NOT DONE
struct ColorClassicStruct { struct ColorClassicStruct {
// TODO: Something goes here // TODO: Something goes here
name: String,
hex: String,
} }
struct ColorTupleStruct(/* TODO: Something goes here */); struct ColorTupleStruct(String, String /* TODO: Something goes here */);
#[derive(Debug)] #[derive(Debug)]
struct UnitStruct; struct UnitStruct;
@ -19,7 +19,10 @@ mod tests {
#[test] #[test]
fn classic_c_structs() { fn classic_c_structs() {
// TODO: Instantiate a classic c struct! // TODO: Instantiate a classic c struct!
// let green = let green = ColorClassicStruct {
name: String::from("green"),
hex: String::from("#00FF00"),
};
assert_eq!(green.name, "green"); assert_eq!(green.name, "green");
assert_eq!(green.hex, "#00FF00"); assert_eq!(green.hex, "#00FF00");
@ -28,7 +31,7 @@ mod tests {
#[test] #[test]
fn tuple_structs() { fn tuple_structs() {
// TODO: Instantiate a tuple struct! // TODO: Instantiate a tuple struct!
// let green = let green = ColorTupleStruct(String::from("green"), String::from("#00FF00"));
assert_eq!(green.0, "green"); assert_eq!(green.0, "green");
assert_eq!(green.1, "#00FF00"); assert_eq!(green.1, "#00FF00");
@ -37,7 +40,7 @@ mod tests {
#[test] #[test]
fn unit_structs() { fn unit_structs() {
// TODO: Instantiate a unit struct! // TODO: Instantiate a unit struct!
// let unit_struct = let unit_struct = UnitStruct;
let message = format!("{:?}s are fun!", unit_struct); let message = format!("{:?}s are fun!", unit_struct);
assert_eq!(message, "UnitStructs are fun!"); assert_eq!(message, "UnitStructs are fun!");

View File

@ -1,8 +1,6 @@
// structs2.rs // structs2.rs
// Address all the TODOs to make the tests pass! // Address all the TODOs to make the tests pass!
// I AM NOT DONE
#[derive(Debug)] #[derive(Debug)]
struct Order { struct Order {
name: String, name: String,
@ -34,7 +32,12 @@ mod tests {
fn your_order() { fn your_order() {
let order_template = create_order_template(); let order_template = create_order_template();
// TODO: Create your own order using the update syntax and template above! // 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.name, "Hacker in Rust");
assert_eq!(your_order.year, order_template.year); assert_eq!(your_order.year, order_template.year);
assert_eq!(your_order.made_by_phone, order_template.made_by_phone); assert_eq!(your_order.made_by_phone, order_template.made_by_phone);

View File

@ -4,8 +4,6 @@
// Make the code compile and the tests pass! // Make the code compile and the tests pass!
// If you have issues execute `rustlings hint structs3` // If you have issues execute `rustlings hint structs3`
// I AM NOT DONE
#[derive(Debug)] #[derive(Debug)]
struct Package { struct Package {
sender_country: String, sender_country: String,
@ -17,6 +15,7 @@ impl Package {
fn new(sender_country: String, recipient_country: String, weight_in_grams: i32) -> Package { fn new(sender_country: String, recipient_country: String, weight_in_grams: i32) -> Package {
if weight_in_grams <= 0 { if weight_in_grams <= 0 {
// panic statement goes here... // panic statement goes here...
panic!("Panic!")
} else { } else {
Package { Package {
sender_country, sender_country,
@ -26,12 +25,14 @@ impl Package {
} }
} }
fn is_international(&self) -> ??? { fn is_international(&self) -> bool {
// Something goes here... // Something goes here...
self.sender_country.ne(&self.recipient_country)
} }
fn get_fees(&self, cents_per_gram: i32) -> ??? { fn get_fees(&self, cents_per_gram: i32) -> i32 {
// Something goes here... // Something goes here...
cents_per_gram * self.weight_in_grams
} }
} }