mirror of
https://github.com/rust-lang/rustlings.git
synced 2026-01-11 05:09:19 +00:00
structs and enums done
This commit is contained in:
parent
884a0b6a51
commit
d961e4695b
@ -1,11 +1,13 @@
|
||||
// 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,
|
||||
Move,
|
||||
ChangeColor,
|
||||
}
|
||||
|
||||
fn main() {
|
||||
|
||||
@ -1,11 +1,13 @@
|
||||
// enums2.rs
|
||||
// Make me compile! Execute `rustlings hint enums2` for hints!
|
||||
|
||||
// I AM NOT DONE
|
||||
|
||||
#[derive(Debug)]
|
||||
enum Message {
|
||||
// TODO: define the different variants used below
|
||||
Move { x: i32, y: i32 },
|
||||
Echo(String),
|
||||
ChangeColor(u8, u8, u8),
|
||||
Quit,
|
||||
}
|
||||
|
||||
impl Message {
|
||||
|
||||
@ -1,10 +1,18 @@
|
||||
// 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
|
||||
// // 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 {
|
||||
@ -37,6 +45,18 @@ impl State {
|
||||
|
||||
fn process(&mut self, message: Message) {
|
||||
// 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 },
|
||||
color: (0, 0, 0),
|
||||
};
|
||||
state.process(Message::ChangeColor((255, 0, 255)));
|
||||
state.process(Message::Echo(String::from("hello world")));
|
||||
state.process(Message::Move(Point { x: 10, y: 15 }));
|
||||
// // solution0
|
||||
// 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::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);
|
||||
|
||||
assert_eq!(state.color, (255, 0, 255));
|
||||
|
||||
@ -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 /* TODO: Something goes here */);
|
||||
|
||||
#[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!");
|
||||
|
||||
@ -1,8 +1,6 @@
|
||||
// structs2.rs
|
||||
// Address all the TODOs to make the tests pass!
|
||||
|
||||
// I AM NOT DONE
|
||||
|
||||
#[derive(Debug)]
|
||||
struct Order {
|
||||
name: String,
|
||||
@ -34,7 +32,12 @@ 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"),
|
||||
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);
|
||||
|
||||
@ -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!("Panic!")
|
||||
} else {
|
||||
Package {
|
||||
sender_country,
|
||||
@ -26,12 +25,14 @@ impl Package {
|
||||
}
|
||||
}
|
||||
|
||||
fn is_international(&self) -> ??? {
|
||||
fn is_international(&self) -> bool {
|
||||
// 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...
|
||||
cents_per_gram * self.weight_in_grams
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user