From 6d65d88b76987442af01396758d0ec0d61256a9b Mon Sep 17 00:00:00 2001 From: Timothy Warren Date: Wed, 6 Feb 2019 16:32:59 -0500 Subject: [PATCH] Add pattern_matching examples --- .idea/misc.xml | 1 + .idea/rust.iml | 5 + .idea/workspace.xml | 95 ++++++----------- pattern_matching/Cargo.toml | 7 ++ pattern_matching/src/main.rs | 196 +++++++++++++++++++++++++++++++++++ 5 files changed, 242 insertions(+), 62 deletions(-) create mode 100644 pattern_matching/Cargo.toml create mode 100644 pattern_matching/src/main.rs diff --git a/.idea/misc.xml b/.idea/misc.xml index 37d23de..3583a37 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -35,6 +35,7 @@ + diff --git a/.idea/rust.iml b/.idea/rust.iml index 0da18db..1263a66 100644 --- a/.idea/rust.iml +++ b/.idea/rust.iml @@ -141,6 +141,10 @@ + + + + @@ -163,6 +167,7 @@ + diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 5d9f874..450794f 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -2,8 +2,9 @@ - - + + + - + - + @@ -306,11 +281,11 @@ + - @@ -381,24 +356,6 @@ - - - - - - - - - - - - - - - - - - @@ -806,7 +763,7 @@ - + @@ -816,10 +773,24 @@ + + + + + + + - - + + + + + + + + + diff --git a/pattern_matching/Cargo.toml b/pattern_matching/Cargo.toml new file mode 100644 index 0000000..5af7510 --- /dev/null +++ b/pattern_matching/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "pattern_matching" +version = "0.1.0" +authors = ["Timothy Warren "] +edition = "2018" + +[dependencies] diff --git a/pattern_matching/src/main.rs b/pattern_matching/src/main.rs new file mode 100644 index 0000000..493595f --- /dev/null +++ b/pattern_matching/src/main.rs @@ -0,0 +1,196 @@ +struct Point { + x: i32, + y: i32, +} + +struct XYZPoint { + x: i32, + y: i32, + z: i32, +} + +enum Color { + Rgb(i32, i32, i32), + Hsv(i32, i32, i32) +} + +enum Message { + Quit, + Move { x: i32, y: i32 }, + Write(String), + ChangeColor(Color), +} + +fn match_variable_shadowing () { + let x = Some(5); + let y = 10; + + match x { + Some(50) => println!("Got 50"), + + // shadows the outer y, gets the value of x + Some(y) => println!("Matched, y = {:?}", y), + + _ => println!("Default case, x = {:?}", x), + } + + println!("at the end: x = {:?}, y = {:?}", x, y); +} + +fn match_multiple () { + let x = 1; + + match x { + 1 | 2 => println!("one or two"), + 3 => println!("three"), + _ => println!("anything"), + } +} + +fn match_range () { + let x = 5; + + match x { + 1 ... 5 => println!("one through five"), + _ => println!("something else"), + } +} + +fn match_destructure_with_literals () { + let p = Point { x: 0, y: 7 }; + + match p { + Point { x, y: 0 } => println!("On the x axis at {}", x), + Point { x: 0, y } => println!("On the y axis at {}", y), + Point { x, y } => println!("On neither axis: ({}, {})", x, y), + } +} + +fn match_destructure_enum () { + let msg = Message::ChangeColor(Color::Hsv(0, 160, 255)); + + match msg { + Message::ChangeColor(Color::Rgb(r, g, b)) => { + println!( + "Change the color to red {}, green {}, and blue {}", + r, + g, + b + ) + }, + Message::ChangeColor(Color::Hsv(h, s, v)) => { + println!( + "Change the color to hue {}, saturation {}, and value {}", + h, + s, + v + ) + } + _ => () + } +} + +fn destructure_references() { + let points = vec![ + Point { x: 0, y: 0 }, + Point { x: 1, y: 5 }, + Point { x: 10, y: -3 }, + ]; + + let sum_of_squares: i32 = points + .iter() + .map(|&Point { x, y }| x * x + y * y) + .sum(); +} + +fn match_ignore_value () { + let mut setting_value = Some(5); + let new_setting_value = Some(10); + + match (setting_value, new_setting_value) { + (Some(_), Some(_)) => { + println!("Can't overwrite an existing customized value"); + } + _ => { + setting_value = new_setting_value; + } + } + + println!("setting is {:?}", setting_value); +} + +fn match_ignore_value_parts () { + let origin = XYZPoint { x: 0, y: 0, z: 0 }; + + match origin { + XYZPoint { x, .. } => println!("x is {}", x), + } +} + +fn match_ignore_value_parts_tuple () { + let numbers = (2, 4, 8, 16, 32); + + match numbers { + (first, .., last) => { + println!("Some numbers: {}, {}", first, last); + }, + } +} + +fn match_guard() { + let num = Some(4); + + match num { + Some(x) if x < 5 => println!("less than five: {}", x), // Has a match guard + Some(x) => println!("{}", x), + None => (), + } +} + +fn match_variable_shadowing_fixed_with_match_guard() { + let x = Some(5); + let y = 10; + + match x { + Some(50) => println!("Got 50"), + Some(n) if n == y => println!("Matched, n = {:?}", n), + _ => println!("Default case, x = {:?}", x), + } + + println!("at the end: x = {:?}, y = {:?}", x, y); +} + +fn at_match_var_bind() { + enum Message { + Hello { id: i32 }, + } + + let msg = Message::Hello { id: 5 }; + + match msg { + Message::Hello { id: id_variable @ 3...7 } => { // The @ binds the value and matches + println!("Found an id in range: {}", id_variable) + }, + Message::Hello { id: 10...12 } => { + println!("Found an id in another range") + }, + Message::Hello { id } => { + println!("Found some other id: {}", id) + }, + } +} + +fn main() { + match_variable_shadowing(); + match_multiple(); + match_range(); + match_destructure_with_literals(); + match_destructure_enum(); + destructure_references(); + match_ignore_value(); + match_ignore_value_parts(); + match_ignore_value_parts_tuple(); + match_guard(); + match_variable_shadowing_fixed_with_match_guard(); + at_match_var_bind(); +}