Solutions: Part 2

This commit is contained in:
vasu 2020-12-30 23:55:31 +01:00
parent 809271493c
commit 7612538d61
8 changed files with 29 additions and 27 deletions

View file

@ -1,11 +1,12 @@
// 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,
Move,
ChangeColor,
} }
fn main() { fn main() {

View file

@ -1,11 +1,12 @@
// 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 Move{ x:i32, y:i32},
Echo(String),
ChangeColor(i32, i32, i32),
Quit,
} }
impl Message { impl Message {

View file

@ -1,10 +1,11 @@
// 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 Move(Point),
Echo(String),
ChangeColor((u8, u8, u8)),
Quit,
} }
struct Point { struct Point {
@ -36,7 +37,16 @@ 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 match message {
Message::ChangeColor(color) => {
self.color = color;
},
Message::Echo(_) => {},
Message::Quit => {self.quit = true;},
Message::Move (position) => {
self.position = position;
},
}
} }
} }

View file

@ -1,10 +1,8 @@
// modules1.rs // modules1.rs
// Make me compile! Execute `rustlings hint modules1` for hints :) // Make me compile! Execute `rustlings hint modules1` for hints :)
// I AM NOT DONE
mod sausage_factory { mod sausage_factory {
fn make_sausage() { pub fn make_sausage() {
println!("sausage!"); println!("sausage!");
} }
} }

View file

@ -7,8 +7,6 @@
// we expect to get when we call `times_two` with a negative number. // we expect to get when we call `times_two` with a negative number.
// No hints, you can do this :) // No hints, you can do this :)
// I AM NOT DONE
pub fn times_two(num: i32) -> i32 { pub fn times_two(num: i32) -> i32 {
num * 2 num * 2
} }
@ -19,11 +17,11 @@ mod tests {
#[test] #[test]
fn returns_twice_of_positive_numbers() { fn returns_twice_of_positive_numbers() {
assert_eq!(times_two(4), ???); assert_eq!(times_two(4), 8);
} }
#[test] #[test]
fn returns_twice_of_negative_numbers() { fn returns_twice_of_negative_numbers() {
// TODO write an assert for `times_two(-4)` assert_eq!(times_two(-4), -8);
} }
} }

View file

@ -6,12 +6,10 @@
// This test has a problem with it -- make the test compile! Make the test // This test has a problem with it -- make the test compile! Make the test
// pass! Make the test fail! Execute `rustlings hint tests1` for hints :) // pass! Make the test fail! Execute `rustlings hint tests1` for hints :)
// I AM NOT DONE
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
#[test] #[test]
fn you_can_assert() { fn you_can_assert() {
assert!(); assert!(true);
} }
} }

View file

@ -2,12 +2,10 @@
// This test has a problem with it -- make the test compile! Make the test // This test has a problem with it -- make the test compile! Make the test
// pass! Make the test fail! Execute `rustlings hint tests2` for hints :) // pass! Make the test fail! Execute `rustlings hint tests2` for hints :)
// I AM NOT DONE
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
#[test] #[test]
fn you_can_assert_eq() { fn you_can_assert_eq() {
assert_eq!(); assert_eq!(2+2, 4);
} }
} }

View file

@ -4,8 +4,6 @@
// we expect to get when we call `is_even(5)`. // we expect to get when we call `is_even(5)`.
// Execute `rustlings hint tests3` for hints :) // Execute `rustlings hint tests3` for hints :)
// I AM NOT DONE
pub fn is_even(num: i32) -> bool { pub fn is_even(num: i32) -> bool {
num % 2 == 0 num % 2 == 0
} }
@ -16,11 +14,11 @@ mod tests {
#[test] #[test]
fn is_true_when_even() { fn is_true_when_even() {
assert!(); assert!(is_even(44));
} }
#[test] #[test]
fn is_false_when_odd() { fn is_false_when_odd() {
assert!(); assert!(!is_even(29));
} }
} }