From dab55f5a6f72ec8487093f4a7e9c1806951c288b Mon Sep 17 00:00:00 2001 From: Tyrel Souza Date: Sat, 27 May 2023 00:27:38 -0400 Subject: [PATCH] Chapter 7 mid way --- .vimspector.json | 10 ++++ chapter6/coins/src/main.rs | 70 ++++++++++++++++++++++++-- chapter6/matchoptions/.vimspector.json | 10 ++++ chapter6/matchoptions/Cargo.lock | 7 +++ chapter6/matchoptions/Cargo.toml | 8 +++ chapter6/matchoptions/src/main.rs | 13 +++++ chapter7/my-project/Cargo.toml | 8 +++ chapter7/my-project/src/main.rs | 3 ++ chapter7/restaurant/Cargo.lock | 7 +++ chapter7/restaurant/Cargo.toml | 8 +++ chapter7/restaurant/src/lib.rs | 48 ++++++++++++++++++ 11 files changed, 188 insertions(+), 4 deletions(-) create mode 100644 chapter6/matchoptions/.vimspector.json create mode 100644 chapter6/matchoptions/Cargo.lock create mode 100644 chapter6/matchoptions/Cargo.toml create mode 100644 chapter6/matchoptions/src/main.rs create mode 100644 chapter7/my-project/Cargo.toml create mode 100644 chapter7/my-project/src/main.rs create mode 100644 chapter7/restaurant/Cargo.lock create mode 100644 chapter7/restaurant/Cargo.toml create mode 100644 chapter7/restaurant/src/lib.rs diff --git a/.vimspector.json b/.vimspector.json index a18cdd4..915bba8 100644 --- a/.vimspector.json +++ b/.vimspector.json @@ -28,3 +28,13 @@ } } } +-e { "configurations": { "launch": { + "adapter": "CodeLLDB", + "filetypes": [ "rust" ], + "configuration": { + "request": "launch", + "program": "${workspaceRoot}/target/debug/HERE" + } + } + } + } diff --git a/chapter6/coins/src/main.rs b/chapter6/coins/src/main.rs index d95cf4c..ca3a151 100644 --- a/chapter6/coins/src/main.rs +++ b/chapter6/coins/src/main.rs @@ -1,16 +1,76 @@ +#[derive(Debug)] +enum UsState { + Alabama, + Alaska, + Arizona, + Arkansas, + California, + Colorado, + Connecticut, + Delaware, + DC, + Florida, + Georgia, + Hawaii, + Idaho, + Illinois, + Indiana, + Iowa, + Kansas, + Kentucky, + Louisiana, + Maine, + Maryland, + Massachusetts, + Michigan, + Minnesota, + Mississippi, + Missouri, + Montana, + Nebraska, + Nevada, + NewHampshire, + NewJersey, + NewMexico, + NewYork, + NorthCarolina, + NorthDakota, + Ohio, + Oklahoma, + Oregon, + Pennsylvania, + RhodeIsland, + SouthCarolina, + SouthDakota, + Tennessee, + Texas, + Utah, + Vermont, + Virginia, + Washington, + WestVirginia, + Wisconsin, + Wyoming +} + enum Coin { Penny, Nickel, Dime, - Quarter, + Quarter(UsState), } fn value_in_cents(coin: Coin) -> u8 { match coin { - Coin::Penny => 1, + Coin::Penny => { + 1 + } Coin::Nickel => 5, Coin::Dime => 10, - Coin::Quarter => 25, + Coin::Quarter(state) => { + println!("State quarter from {:?}!", state); + 25 + } } } @@ -20,5 +80,7 @@ fn main() { let v: u8 = value_in_cents(Coin::Dime); let s: String = v.to_string(); println!("{}", s); - + let vq: u8 = value_in_cents(Coin::Quarter(UsState::NewHampshire)); + let sq: String = vq.to_string(); + println!("{}", sq); } diff --git a/chapter6/matchoptions/.vimspector.json b/chapter6/matchoptions/.vimspector.json new file mode 100644 index 0000000..0a80601 --- /dev/null +++ b/chapter6/matchoptions/.vimspector.json @@ -0,0 +1,10 @@ +{ "configurations": { "launch": { + "adapter": "CodeLLDB", + "filetypes": [ "rust" ], + "configuration": { + "request": "launch", + "program": "${workspaceRoot}/target/debug/matchoptions" + } + } + } + } diff --git a/chapter6/matchoptions/Cargo.lock b/chapter6/matchoptions/Cargo.lock new file mode 100644 index 0000000..d47d1c7 --- /dev/null +++ b/chapter6/matchoptions/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "matchoptions" +version = "0.1.0" diff --git a/chapter6/matchoptions/Cargo.toml b/chapter6/matchoptions/Cargo.toml new file mode 100644 index 0000000..1812809 --- /dev/null +++ b/chapter6/matchoptions/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "matchoptions" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/chapter6/matchoptions/src/main.rs b/chapter6/matchoptions/src/main.rs new file mode 100644 index 0000000..a81900c --- /dev/null +++ b/chapter6/matchoptions/src/main.rs @@ -0,0 +1,13 @@ +fn plus_one(x: Option) -> Option { + match x { + None => None, + Some(i) => Some(i+1), + } +} + + +fn main() { + let five = Some(5); + let six = plus_one(five); + let none = plus_one(None); +} diff --git a/chapter7/my-project/Cargo.toml b/chapter7/my-project/Cargo.toml new file mode 100644 index 0000000..aebde11 --- /dev/null +++ b/chapter7/my-project/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "my-project" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/chapter7/my-project/src/main.rs b/chapter7/my-project/src/main.rs new file mode 100644 index 0000000..e7a11a9 --- /dev/null +++ b/chapter7/my-project/src/main.rs @@ -0,0 +1,3 @@ +fn main() { + println!("Hello, world!"); +} diff --git a/chapter7/restaurant/Cargo.lock b/chapter7/restaurant/Cargo.lock new file mode 100644 index 0000000..ba86874 --- /dev/null +++ b/chapter7/restaurant/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "restaurant" +version = "0.1.0" diff --git a/chapter7/restaurant/Cargo.toml b/chapter7/restaurant/Cargo.toml new file mode 100644 index 0000000..678b365 --- /dev/null +++ b/chapter7/restaurant/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "restaurant" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/chapter7/restaurant/src/lib.rs b/chapter7/restaurant/src/lib.rs new file mode 100644 index 0000000..cbebbff --- /dev/null +++ b/chapter7/restaurant/src/lib.rs @@ -0,0 +1,48 @@ +pub mod front_of_house { + pub mod hosting { + pub fn add_to_waitlist() {} + pub fn seat_at_table() {} + } + pub mod serving { + pub fn take_order() {} + pub fn serve_order() {} + pub fn take_payent() {} + } +} +fn deliver_order() {} + +mod back_of_house { + pub struct Breakfast { + pub toast: String, + seasonal_fruit: String, + } + pub enum Appetizer { + Soup, + Salad, + } + impl Breakfast { + pub fn summer(toast: &str) -> Breakfast { + Breakfast { + toast: String::from(toast), + seasonal_fruit:String::from("Peaches"), + } + } + } + + fn fix_incorrect_order(){ + cook_order(); + super::deliver_order(); + } + fn cook_order(){} +} + + +pub fn eat_at_restaurant() { + let mut meal = back_of_house::Breakfast::summer("rye"); + meal.toast = String::from("Wheat"); + println!("I'd like {} toast please", meal.toast); + + let _order1 = back_of_house::Appetizer::Soup; + let _order2 = back_of_house::Appetizer::Salad; + ; +}