summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAdam Carpenter <53hornet@gmail.com>2019-03-21 13:20:03 -0400
committerAdam Carpenter <53hornet@gmail.com>2019-03-21 13:20:03 -0400
commit2fadb006603139c335ed8df8fa9c25402986c4fd (patch)
treefcc514f965f590f772ce212a4dd1e9c07d866bf3
parent7daad878e8c34997d981a7afc37f59f373fccf29 (diff)
downloadlearning-rust-2fadb006603139c335ed8df8fa9c25402986c4fd.tar.xz
learning-rust-2fadb006603139c335ed8df8fa9c25402986c4fd.zip
Finished patterns.
-rw-r--r--patterns/src/main.rs329
1 files changed, 311 insertions, 18 deletions
diff --git a/patterns/src/main.rs b/patterns/src/main.rs
index c4d9d65..4ee101d 100644
--- a/patterns/src/main.rs
+++ b/patterns/src/main.rs
@@ -1,23 +1,316 @@
fn main() {
- let favorite_color: Option<&str> = None;
- let is_tuesday = false;
- let age: Result<u8, _> = "34".parse();
+// let favorite_color: Option<&str> = None;
+// let is_tuesday = false;
+// let age: Result<u8, _> = "34".parse();
+//
+// if let Some(color) = favorite_color {
+// dbg!(color);
+// }
+// else if is_tuesday {
+// dbg!("green");
+// }
+// else if let Ok(age) = age {
+// if age > 30 {
+// dbg!("purple");
+// }
+// else {
+// dbg!("orange");
+// }
+// }
+// else {
+// dbg!("blue");
+// }
- if let Some(color) = favorite_color {
- dbg!(color);
- }
- else if is_tuesday {
- dbg!("green");
- }
- else if let Ok(age) = age {
- if age > 30 {
- dbg!("purple");
- }
- else {
- dbg!("orange");
- }
+
+// let mut stack = Vec::new();
+//
+// stack.push(1);
+// stack.push(2);
+// stack.push(3);
+//
+// while let Some(top) = stack.pop() {
+// dbg!(top);
+// }
+
+
+// let v = vec!['a', 'b', 'c'];
+//
+// for (_, value) in v.iter().enumerate() {
+// //println!("{} is at index {}", value, index);
+// dbg!(value);
+// }
+
+
+// let (..) = (1, 2, 3);
+
+
+// let point = (3, 5);
+// print_coords(&point);
+
+
+// let Some(x) = Some(5);
+// if let Some(x) = Some(5) {
+// dbg!(x);
+// }
+// if let x = 5 {
+// dbg!(x);
+// };
+
+
+// let x = 1;
+//
+// match x {
+// 1 => dbg!("one"),
+// 2 => dbg!("two"),
+// 3 => dbg!("three"),
+// _ => dbg!("else"),
+// };
+
+
+
+// let x = Some(5);
+// let y = 10;
+//
+// match x {
+// Some(50) => println!("got 50"),
+// Some(y) => println!("matched, y == {:?}", y),
+// _ => println!("default: x == {:?}", x),
+// }
+//
+// println!("end: x == {:?}, y == {:?}", x, y);
+
+
+// let x = 5;
+//
+// match x {
+// 1 ... 5 => println!("one through five"),
+// _ => println!("else"),
+// }
+
+
+// let x = 'w';
+//
+// match x {
+// 'a' ... 'j' => println!("early ascii char"),
+// 'k' ... 'z' => println!("late ascii char"),
+// _ => println!("else"),
+// }
+
+
+
+// let p = Point { x: 0, y: 7 };
+// //let Point { x: a, y: b } = p;
+// let Point { x, y } = p;
+// assert_eq!(0, x);
+// assert_eq!(7, y);
+
+
+// let p = Point { x: 1, y: 0 };
+//
+// match p {
+// Point { x, y: 0 } => dbg!("x-axis"),
+// Point { x: 0, y } => dbg!("y-axis"),
+// Point { x, y } => dbg!("neither"),
+// };
+
+
+// //let msg = Message::ChangeColor(0, 160, 255);
+// //let msg = Message::Write(String::from("hi there"));
+// //let msg = Message::Move {
+// // x: 1,
+// // y: 2,
+// //};
+// let msg = Message::ChangeColor(Color::Hsv(0, 160, 255));
+//
+// match msg {
+// Message::Quit => {
+// println!("the quit variant has no data to destructure.")
+// },
+// Message::Move { x, y } => {
+// println!(
+// "Move in the direction {}, {}",
+// x,
+// y
+// );
+// }
+// Message::Write(text) => println!("text message: {}", text),
+// Message::ChangeColor(Color::Rgb(r, g, b)) => {
+// println!(
+// "Change the color to RGB {}{}{}",
+// r,
+// g,
+// b
+// )
+// }
+// Message::ChangeColor(Color::Hsv(h, s, v)) => {
+// println!(
+// "Change the color to HSV {}{}{}",
+// h,
+// s,
+// v
+// )
+// }
+// }
+
+
+// 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();
+// dbg!(sum_of_squares);
+//
+//
+// let ((feet, inches), Point { x, y }) = ((3, 10), Point { x: 3, y: -10 });
+
+
+// foo(3, 4);
+
+
+// let mut setting_value = Some(5);
+// //let new_setting_value = Some(10);
+// let new_setting_value = None;
+//
+// match (setting_value, new_setting_value) {
+// (Some(_), Some(_)) => {
+// println!("can't overwrite existing customized value.");
+// }
+// _ => {
+// setting_value = new_setting_value;
+// }
+// }
+//
+// println!("setting is {:?}", setting_value);
+//
+//
+// let numbers = (2, 4, 8, 16, 32);
+//
+// match numbers {
+// (first, _, third, _, fifth) => {
+// println!("numbers: {}, {}, {}", first, third, fifth)
+// },
+// }
+//
+//
+// let _x = 5;
+// let _y = 10;
+//
+//
+// let s = Some(String::from("hello"));
+//
+// if let Some(_) = s {
+// println!("found a string");
+// }
+//
+// println!("{:?}", s);
+
+
+// let origin = Point { x: 1, y: 2, z: 3 };
+//
+// match origin {
+// Point {x, .. } => dbg!(x),
+// };
+//
+// let numbers = (2, 4, 8, 16, 32);
+//
+// match numbers {
+// (first, .., last) => {
+// dbg!(first);
+// dbg!(last);
+// },
+// }
+
+
+// let num = Some(4);
+//
+// let test = match num {
+// Some(x) if x < 5 => println!("{}", x),
+// Some(x) => println!("{}", x),
+// None => (),
+// };
+
+
+// let x = Some(10);
+// let y = 10;
+//
+// match x {
+// Some(50) => println!("got 50"),
+// Some(n) if n == y => println!("{:?}", n),
+// _ => println!("{:?}", x),
+// }
+//
+// println!("{:?} {:?}", x, y);
+
+
+// let x = 4;
+// let y = false;
+//
+// match x {
+// 4 | 5 | 6 if y => println!("yes"),
+// _ => println!("no"),
+// }
+
+
+ enum Message {
+ Hello { id: i32 },
}
- else {
- dbg!("blue");
+
+ let msg = Message::Hello { id: 13 };
+
+ match msg {
+ Message::Hello { id: id_variable @ 3...7 } => {
+ println!("{}", id_variable)
+ },
+ Message::Hello { id: 10...12 } => {
+ println!("another range")
+ },
+ Message::Hello { id } => {
+ println!("else")
+ },
}
+
+
+ println!("Done.");
}
+
+
+//struct Point {
+// x: i32,
+// y: i32,
+// z: i32,
+//}
+
+
+//fn foo(_: i32, y: i32) {
+// println!("this code only uses y: {}", y);
+//}
+
+
+//fn print_coords(&(x, y): &(i32, i32)) {
+// println!("{}, {}", x, y);
+//}
+
+
+//struct Point {
+// x: i32,
+// y: i32,
+//}
+
+
+//enum Message {
+// Quit,
+// Move { x: i32, y: i32 },
+// Write(String),
+// ChangeColor(Color),
+//}
+//
+//
+//enum Color {
+// Rgb(i32, i32, i32),
+// Hsv(i32, i32, i32)
+//}