my rustlng answer 5.1 ~ 6

Signed-off-by: IloveKanade <xjlgxlgx@gmail.com>
This commit is contained in:
IloveKanade 2022-04-29 15:36:54 +08:00
parent 4adfa82e1a
commit 81ba4c7075
6 changed files with 46 additions and 22 deletions

View File

@ -1,16 +1,18 @@
// 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 Quit,
Echo { x: i32, y: i32 },
Move(String),
ChangeColor(i32, i32, i32),
} }
fn main() { fn main() {
println!("{:?}", Message::Quit); println!("{:?}", Message::Quit);
println!("{:?}", Message::Echo); println!("{:?}", Message::Echo{x:1,y:2});
println!("{:?}", Message::Move); println!("{:?}", Message::Move(String::from("dddd")));
println!("{:?}", Message::ChangeColor); println!("{:?}", Message::ChangeColor(255,255, 0));
} }

View File

@ -6,6 +6,10 @@
#[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(i32, i32, i32),
Quit,
} }
impl Message { impl Message {

View File

@ -1,10 +1,12 @@
// 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
ChangeColor(u8, u8, u8),
Echo(String),
Move{x: u8, y: u8},
Quit
} }
struct Point { struct Point {
@ -37,6 +39,12 @@ 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 {
Message::ChangeColor(r, g, b) => self.change_color((r, g, b)),
Message::Echo(s) => self.echo(s),
Message::Move{x, y} => self.move_position(Point{x, y}),
Message::Quit => self.quit()
}
} }
} }
@ -51,9 +59,9 @@ 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))); state.process(Message::ChangeColor(255, 0, 255));
state.process(Message::Echo(String::from("hello world"))); state.process(Message::Echo(String::from("hello world")));
state.process(Message::Move(Point { x: 10, y: 15 })); state.process(Message::Move{ 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);
#[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

@ -34,7 +34,11 @@ 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: "Hacker in Rust".to_string(),
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!("wtf,weight is less than zero")
} else { } else {
Package { Package {
sender_country, sender_country,
@ -26,12 +25,16 @@ impl Package {
} }
} }
fn is_international(&self) -> ??? { fn is_international(&self) -> bool {
// Something goes here... if self.sender_country != self.recipient_country {
true
} else {
false
}
} }
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
} }
} }