From ca812add166befd3e0b6f419114b6777e8847419 Mon Sep 17 00:00:00 2001 From: Dan Buch Date: Wed, 27 Sep 2023 23:16:10 -0400 Subject: [PATCH] Much more flow control destructuring in RBE --- .../destructuring.d/destructure_enum.rs | 30 +++++++++++++++++++ .../destructuring.d/destructure_pointers.rs | 29 ++++++++++++++++++ .../destructuring.d/destructure_slice.rs | 27 +++++++++++++++++ .../destructuring.d/destructure_structures.rs | 19 ++++++++++++ .../destructuring.d/destructure_tuple.rs | 12 ++++++++ rustbyexample/flow_control.d/match.d/guard.rs | 16 ++++++++++ .../flow_control.d/match.d/guard2.rs | 9 ++++++ rustbyexample/flow_control.d/match.rs | 19 ++++++++++++ 8 files changed, 161 insertions(+) create mode 100644 rustbyexample/flow_control.d/match.d/destructuring.d/destructure_enum.rs create mode 100644 rustbyexample/flow_control.d/match.d/destructuring.d/destructure_pointers.rs create mode 100644 rustbyexample/flow_control.d/match.d/destructuring.d/destructure_slice.rs create mode 100644 rustbyexample/flow_control.d/match.d/destructuring.d/destructure_structures.rs create mode 100644 rustbyexample/flow_control.d/match.d/destructuring.d/destructure_tuple.rs create mode 100644 rustbyexample/flow_control.d/match.d/guard.rs create mode 100644 rustbyexample/flow_control.d/match.d/guard2.rs create mode 100644 rustbyexample/flow_control.d/match.rs diff --git a/rustbyexample/flow_control.d/match.d/destructuring.d/destructure_enum.rs b/rustbyexample/flow_control.d/match.d/destructuring.d/destructure_enum.rs new file mode 100644 index 0000000..fe7020a --- /dev/null +++ b/rustbyexample/flow_control.d/match.d/destructuring.d/destructure_enum.rs @@ -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 + ), + } +} diff --git a/rustbyexample/flow_control.d/match.d/destructuring.d/destructure_pointers.rs b/rustbyexample/flow_control.d/match.d/destructuring.d/destructure_pointers.rs new file mode 100644 index 0000000..c49c63f --- /dev/null +++ b/rustbyexample/flow_control.d/match.d/destructuring.d/destructure_pointers.rs @@ -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); + } + } +} diff --git a/rustbyexample/flow_control.d/match.d/destructuring.d/destructure_slice.rs b/rustbyexample/flow_control.d/match.d/destructuring.d/destructure_slice.rs new file mode 100644 index 0000000..5fb096a --- /dev/null +++ b/rustbyexample/flow_control.d/match.d/destructuring.d/destructure_slice.rs @@ -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 + ), + } +} diff --git a/rustbyexample/flow_control.d/match.d/destructuring.d/destructure_structures.rs b/rustbyexample/flow_control.d/match.d/destructuring.d/destructure_structures.rs new file mode 100644 index 0000000..b469e69 --- /dev/null +++ b/rustbyexample/flow_control.d/match.d/destructuring.d/destructure_structures.rs @@ -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}"); +} diff --git a/rustbyexample/flow_control.d/match.d/destructuring.d/destructure_tuple.rs b/rustbyexample/flow_control.d/match.d/destructuring.d/destructure_tuple.rs new file mode 100644 index 0000000..ce8c8c5 --- /dev/null +++ b/rustbyexample/flow_control.d/match.d/destructuring.d/destructure_tuple.rs @@ -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"), + } +} diff --git a/rustbyexample/flow_control.d/match.d/guard.rs b/rustbyexample/flow_control.d/match.d/guard.rs new file mode 100644 index 0000000..27c90a9 --- /dev/null +++ b/rustbyexample/flow_control.d/match.d/guard.rs @@ -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), + } +} diff --git a/rustbyexample/flow_control.d/match.d/guard2.rs b/rustbyexample/flow_control.d/match.d/guard2.rs new file mode 100644 index 0000000..49b8684 --- /dev/null +++ b/rustbyexample/flow_control.d/match.d/guard2.rs @@ -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."), + } +} diff --git a/rustbyexample/flow_control.d/match.rs b/rustbyexample/flow_control.d/match.rs new file mode 100644 index 0000000..4be9d13 --- /dev/null +++ b/rustbyexample/flow_control.d/match.rs @@ -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); +}