From 0e2995057bfb9dc974b464a660ccff9228497da2 Mon Sep 17 00:00:00 2001 From: James Zow Date: Fri, 13 May 2022 00:59:38 +0800 Subject: [PATCH] add two match test --- exercises/match/match1.rs | 101 ++++++++++++++++++++++++++++++++++++++ exercises/match/match2.rs | 35 +++++++++++++ 2 files changed, 136 insertions(+) create mode 100644 exercises/match/match1.rs create mode 100644 exercises/match/match2.rs diff --git a/exercises/match/match1.rs b/exercises/match/match1.rs new file mode 100644 index 00000000..2a782126 --- /dev/null +++ b/exercises/match/match1.rs @@ -0,0 +1,101 @@ +// match1.rs +// Make me compile! Execute `rustlings hint match1` for hints :) + +// Use the match of the official library for basic matching, tuple matching, +// structure matching and enumeration matching. + +// I AM NOT DONE + +/// +/// Basic match test +/// +fn basic_match_test(){ + let number = 70; + + println!("The number is:{}", number); + match number { + 1 => println!("1"), + 5 | 7 | 8 | 9 => println!("include 5 7 8 9"), + 13..=22 => println!("include 20"), + _ => println!("other"), + } + + let flag = true; + let status = match flag { + false => 0, + true => 1, + }; + println!("state: {} -> {}", flag, status) +} + +/// +/// Match test of tuples tuple type +/// +fn tuples_match_test(){ + let age = (15,22,33); + match age { + (7 , _y ,_z) => println!("{:?}:include age of 7", age), + (15, ..) => println!("{:?}:age matching from over 15", age), + _ => println!("{:?}:other age", age) + } +} + +/// +/// Match test of enum type +/// +enum Color{ + RED, + BLUE, + GREEN, + + RGB(u32, u32, u32), + HSV(u32, u32, u32), + CMY(u32, u32, u32), +} +enum HSV{ + HSV(u32, u32, u32), +} + +fn enums_match_test(){ + let color = Color::RGB(178, 50, 32); + let hsv = HSV::HSV(5, 32, 66); + + match color { + Color::RED => println!("red"), + Color::BLUE => println!("blue"), + Color::GREEN => println!("green"), + + Color::RGB(r, g,b) => + println!("RGB Color R:{}, G:{}, B:{}", r, g, b ), + Color::HSV(h,s,v) => + println!("HSV H:{:}, S:{:}, V:{:}", h, s, v), + Color::CMY(c,m,y) => + println!("CMY C:{:?}, M:{:?}, Y:{:?}",c, m, y) + } + + match hsv { HSV::HSV(h,s,v) => + println!("HSV H:{:}, S:{:}, V:{:}", h, s, v), + } +} + +/// +/// Structs structure match test +/// +#[test] +fn structs_match_test(){ + struct Foo{ + x: (u32, u32), + y: u32, + } + + let foo = Foo{ + x : (6, 8), y : 20 + }; + + match foo { + Foo { x: (1, b), y } + => println!("The first number of X is 1, b = {}, y = {} ", b, y), + Foo { y: 2, x: i } => println!("y is 2, i = {:?}", i), + Foo { y, .. } => println!("y = {}, It has nothing to do with X", y), + } +} \ No newline at end of file diff --git a/exercises/match/match2.rs b/exercises/match/match2.rs new file mode 100644 index 00000000..ede16b0c --- /dev/null +++ b/exercises/match/match2.rs @@ -0,0 +1,35 @@ +// match2.rs +// Make me compile! Execute `rustlings hint match2` for hints :) + +// You can add filters to match. + +// I AM NOT DONE + +/// +/// Filters one match test +/// +fn filters_one(){ + + let pair = (6, -5); + + match pair { + (x , y) if x == y => println!("The two values are equal"), + (x , y) if x + y == 0 => println!("two values are added"), + (x , y) if x - y == 11 => println!("positive number"), + _ => println!("No match found"), + } +} + +/// +/// Filters two match test +/// +fn filters_two(){ + + let num : u8 = 9; + + match num { + i if i == 0 => println!("0"), + i if i == 8 => println!("8"), + _ => println!("No match found") + } +} \ No newline at end of file