structs, enums

This commit is contained in:
palutz 2023-07-23 17:49:24 +01:00
parent 20000a4578
commit 13a3b6dd34
6 changed files with 59 additions and 21 deletions

View File

@ -2,11 +2,12 @@
// //
// No hints this time! ;) // No hints this time! ;)
// I AM NOT DONE
#[derive(Debug)] #[derive(Debug)]
enum Message { enum Message {
// TODO: define a few types of messages as used below Quit,
Echo,
Move,
ChangeColor,
} }
fn main() { fn main() {

View File

@ -3,11 +3,13 @@
// Execute `rustlings hint enums2` or use the `hint` watch subcommand for a // Execute `rustlings hint enums2` or use the `hint` watch subcommand for a
// hint. // hint.
// 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(i32, i32, i32),
Quit,
} }
impl Message { impl Message {

View File

@ -5,10 +5,12 @@
// Execute `rustlings hint enums3` or use the `hint` watch subcommand for a // Execute `rustlings hint enums3` or use the `hint` watch subcommand for a
// hint. // hint.
// 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(Point),
Quit,
} }
struct Point { struct Point {
@ -43,6 +45,12 @@ impl State {
// variants // variants
// Remember: When passing a tuple as a function argument, you'll need // Remember: When passing a tuple as a function argument, you'll need
// extra parentheses: fn function((t, u, p, l, e)) // extra parentheses: fn function((t, u, p, l, e))
match message {
Message::ChangeColor(a, b, c) => self.change_color((a, b, c)),
Message::Echo(msg) => self.echo(msg),
Message::Move(p) => self.move_position(p),
Message::Quit => self.quit(),
}
} }
} }

View File

@ -5,17 +5,21 @@
// Execute `rustlings hint structs1` or use the `hint` watch subcommand for a // Execute `rustlings hint structs1` or use the `hint` watch subcommand for a
// hint. // hint.
// I AM NOT DONE
struct ColorClassicStruct { struct ColorClassicStruct {
// TODO: Something goes here // TODO: Something goes here
red : i32,
green : i32,
blue : i32,
} }
struct ColorTupleStruct(/* TODO: Something goes here */); struct ColorTupleStruct(i32, i32, i32);
#[derive(Debug)] #[derive(Debug)]
struct UnitLikeStruct; struct UnitLikeStruct;
#[derive(Debug)]
struct JustStefano;
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
@ -23,7 +27,11 @@ 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 {
red : 0,
green : 255,
blue : 0,
};
assert_eq!(green.red, 0); assert_eq!(green.red, 0);
assert_eq!(green.green, 255); assert_eq!(green.green, 255);
@ -33,7 +41,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 (0, 255, 0);
assert_eq!(green.0, 0); assert_eq!(green.0, 0);
assert_eq!(green.1, 255); assert_eq!(green.1, 255);
@ -43,9 +51,13 @@ mod tests {
#[test] #[test]
fn unit_structs() { fn unit_structs() {
// TODO: Instantiate a unit-like struct! // TODO: Instantiate a unit-like struct!
// let unit_like_struct = let st = JustStefano{};
let unit_like_struct = UnitLikeStruct {};
let message = format!("{:?}s are fun!", unit_like_struct); let message = format!("{:?}s are fun!", unit_like_struct);
let msg2 = format!("{:?}, hello ", st);
assert_eq!(message, "UnitLikeStructs are fun!"); assert_eq!(message, "UnitLikeStructs are fun!");
assert_eq!(msg2, "JustStefano, hello ");
} }
} }

View File

@ -5,7 +5,6 @@
// Execute `rustlings hint structs2` or use the `hint` watch subcommand for a // Execute `rustlings hint structs2` or use the `hint` watch subcommand for a
// hint. // hint.
// I AM NOT DONE
#[derive(Debug)] #[derive(Debug)]
struct Order { struct Order {
@ -30,6 +29,18 @@ fn create_order_template() -> Order {
} }
} }
fn create_my_order () -> Order {
Order {
name: "Hacker in Rust".to_string(),
year: 2019,
made_by_phone: false,
made_by_mobile: false,
made_by_email: true,
item_number: 123,
count: 1,
}
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
@ -38,7 +49,13 @@ 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 = create_my_order();
// or better ...
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

@ -7,8 +7,6 @@
// Execute `rustlings hint structs3` or use the `hint` watch subcommand for a // Execute `rustlings hint structs3` or use the `hint` watch subcommand for a
// hint. // hint.
// I AM NOT DONE
#[derive(Debug)] #[derive(Debug)]
struct Package { struct Package {
sender_country: String, sender_country: String,
@ -29,12 +27,12 @@ impl Package {
} }
} }
fn is_international(&self) -> ??? { fn is_international(&self) -> bool {
// Something goes here... self.sender_country != self.recipient_country
} }
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
} }
} }