Merge pull request from ZhiHanZ/exercise_ans

enum, test, test3
This commit is contained in:
ZhiHanZ 2020-05-03 22:15:51 -07:00 committed by GitHub
commit f06043429f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 32 additions and 15 deletions

View file

@ -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() {

View file

@ -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
Quit,
Echo(String),// a tuple with one elem
Move{x: i32, y: i32},
ChangeColor(u8, u8, u8),
}
impl Message {

View file

@ -1,10 +1,13 @@
// enums3.rs
// Address all the TODOs to make the tests pass!
// I AM NOT DONE
#[derive(Debug)]
enum Message {
// TODO: implement the message variant types based on their usage below
Quit,
Echo(String),// a tuple with one elem
Move{x: u8, y: u8},
ChangeColor(u8, u8, u8),
}
struct Point {
@ -37,6 +40,12 @@ impl State {
fn process(&mut self, message: Message) {
// TODO: create a match expression to process the different message variants
match message {
Message::Quit => {self.quit()}
Message::Move{x, y} => {self.move_position(Point{x: x, y : y})}
Message::Echo(string) => {self.echo(string)}
Message::ChangeColor(r,g,b) => {self.change_color((r,g,b))}
}
}
}

View file

@ -7,7 +7,6 @@
// we expect to get when we call `times_two` with a negative number.
// No hints, you can do this :)
// I AM NOT DONE
pub fn times_two(num: i32) -> i32 {
num * 2
@ -19,11 +18,11 @@ mod tests {
#[test]
fn returns_twice_of_positive_numbers() {
assert_eq!(times_two(4), ???);
assert_eq!(times_two(4), 8);
}
#[test]
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,11 @@
// 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 :)
// I AM NOT DONE
#[cfg(test)]
mod tests {
#[test]
fn you_can_assert() {
assert!();
assert!(!(1==9));
}
}

View file

@ -2,12 +2,11 @@
// 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 :)
// I AM NOT DONE
#[cfg(test)]
mod tests {
#[test]
fn you_can_assert_eq() {
assert_eq!();
assert_eq!(1,1);
}
}

View file

@ -4,7 +4,6 @@
// we expect to get when we call `is_even(5)`.
// Execute `rustlings hint tests3` for hints :)
// I AM NOT DONE
pub fn is_even(num: i32) -> bool {
num % 2 == 0
@ -16,6 +15,14 @@ mod tests {
#[test]
fn is_true_when_even() {
assert!();
assert!(!is_even(5));
}
#[test]
fn is_even_works() ->Result<(), String> {
if !is_even(1<<10) {
Err(String::from("1<<10"))
} else {
Ok(())
}
}
}