mirror of
https://github.com/rust-lang/rustlings.git
synced 2026-01-11 05:09:19 +00:00
add two match test
This commit is contained in:
parent
75b09dd69a
commit
0e2995057b
101
exercises/match/match1.rs
Normal file
101
exercises/match/match1.rs
Normal file
@ -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),
|
||||
}
|
||||
}
|
||||
35
exercises/match/match2.rs
Normal file
35
exercises/match/match2.rs
Normal file
@ -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")
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user