Much more flow control destructuring in RBE

This commit is contained in:
Dan Buch 2023-09-27 23:16:10 -04:00
parent 460741740b
commit ca812add16
Signed by: meatballhat
GPG Key ID: A12F782281063434
8 changed files with 161 additions and 0 deletions

View File

@ -0,0 +1,30 @@
#[allow(dead_code)]
enum Color {
Red,
Blue,
Green,
RGB(u32, u32, u32),
HSV(u32, u32, u32),
HSL(u32, u32, u32),
CMY(u32, u32, u32),
CMYK(u32, u32, u32, u32),
}
fn main() {
let color = Color::RGB(122, 17, 40);
println!("What color is it?");
match color {
Color::Red => println!("The color is Red!"),
Color::Blue => println!("The color is Blue!"),
Color::Green => println!("The color is Green!"),
Color::RGB(r, g, b) => println!("Red: {}, green: {}, and blue: {}!", r, g, b),
Color::HSV(h, s, v) => println!("Hue: {}, saturation: {}, value: {}!", h, s, v),
Color::HSL(h, s, l) => println!("Hue: {}, saturation: {}, lightness: {}!", h, s, l),
Color::CMY(c, m, y) => println!("Cyan: {}, magenta: {}, yellow: {}!", c, m, y),
Color::CMYK(c, m, y, k) => println!(
"Cyan: {}, magenta: {}, yellow: {}, key (black): {}!",
c, m, y, k
),
}
}

View File

@ -0,0 +1,29 @@
fn main() {
let reference = &4;
match reference {
&val => println!("Got a value via destructuring: {:?}", val),
}
match *reference {
val => println!("Got a value via dereferencing: {:?}", val),
}
let _not_a_reference = 3;
let ref _is_a_reference = 3;
let value = 5;
let mut mut_value = 6;
match value {
ref r => println!("Got a reference to a value: {:?}", r),
}
match mut_value {
ref mut m => {
*m += 10;
println!("We added 10. `mut_value`: {:?}", m);
}
}
}

View File

@ -0,0 +1,27 @@
fn main() {
let array = [1, -2, 6];
match array {
[0, second, third] => println!("array[0] = 0, array[1] = {}, array[2] = {}", second, third),
[1, _, third] => println!(
"array[0] = 1, array[2] = {} and array[1] was ignored",
third
),
[-1, second, ..] => println!(
"array[0] = -1, array[1] = {} and all the other ones were ignored",
second
),
[3, second, tail @ ..] => println!(
"array[0] = 3, array[1] = {} and the other elements were {:?}",
second, tail
),
[first, middle @ .., last] => println!(
"array[0] = {}, middle = {:?}, array[2] = {}",
first, middle, last
),
}
}

View File

@ -0,0 +1,19 @@
fn main() {
struct Foo {
x: (u32, u32),
y: u32,
}
let foo = Foo { x: (1, 2), y: 3 };
match foo {
Foo { x: (1, b), y } => println!("First of x is 1, b = {}, y = {}", b, y),
Foo { y: 2, x: i } => println!("y is 2, i = {:?}", i),
Foo { y, .. } => println!("y = {}, we don't care about x", y),
}
let faa = Foo { x: (1, 2), y: 3 };
let Foo { x: x0, y: y0 } = faa;
println!("Outside: x0 = {x0:?}, y0 = {y0}");
}

View File

@ -0,0 +1,12 @@
fn main() {
let triple = (0, -2, 3);
println!("Tell me about {:?}", triple);
match triple {
(0, y, z) => println!("First is `0`, `y` is {:?}, and `z` is {:?}", y, z),
(1, ..) => println!("First is `1` and the rest doesn't matter"),
(.., 2) => println!("last is `2` and the rest doesn't matter"),
(3, .., 4) => println!("First is `3`, last is `4`, and the rest doesn't matter"),
_ => println!("It doesn't matter what they are"),
}
}

View File

@ -0,0 +1,16 @@
#[allow(dead_code)]
enum Temperature {
Celsius(i32),
Fahrenheit(i32),
}
fn main() {
let temperature = Temperature::Celsius(35);
match temperature {
Temperature::Celsius(t) if t > 30 => println!("{}C is above 30 Celsius", t),
Temperature::Celsius(t) => println!("{}C is below 30 Celsius", t),
Temperature::Fahrenheit(t) if t > 86 => println!("{}F is above 86 Fahrenheit", t),
Temperature::Fahrenheit(t) => println!("{}F is below 86 Fahrenheit", t),
}
}

View File

@ -0,0 +1,9 @@
fn main() {
let number: u8 = 4;
match number {
i if i == 0 => println!("Zero"),
i if i > 0 => println!("Greater than zero"),
_ => unreachable!("Should never happen."),
}
}

View File

@ -0,0 +1,19 @@
fn main() {
let number = 13;
println!("Tell me about {}", number);
match number {
1 => println!("One!"),
2 | 3 | 5 | 7 | 11 => println!("This is a prime"),
13..=19 => println!("A teen"),
_ => println!("Ain't special"),
}
let boolean = true;
let binary = match boolean {
false => 0,
true => 1,
};
println!("{} -> {}", boolean, binary);
}