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
// Make me compile! Execute `rustlings hint enums1` for hints!
// I AM NOT DONE
#[derive(Debug)]
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() {
println!("{:?}", Message::Quit);
println!("{:?}", Message::Echo);
println!("{:?}", Message::Move);
println!("{:?}", Message::ChangeColor);
println!("{:?}", Message::Echo{x:1,y:2});
println!("{:?}", Message::Move(String::from("dddd")));
println!("{:?}", Message::ChangeColor(255,255, 0));
}

View File

@ -6,6 +6,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 {

View File

@ -1,10 +1,12 @@
// enums3.rs
// Address all the TODOs to make the tests pass!
// I AM NOT DONE
enum Message {
// 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 {
@ -37,6 +39,12 @@ impl State {
fn process(&mut self, message: Message) {
// 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 },
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::Move(Point { x: 10, y: 15 }));
state.process(Message::Move{ x: 10, y: 15 });
state.process(Message::Quit);
assert_eq!(state.color, (255, 0, 255));

View File

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

View File

@ -34,7 +34,11 @@ 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: "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);

View File

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