Chapter 7 mid way

This commit is contained in:
Tyrel Souza 2023-05-27 00:27:38 -04:00
parent 66f2f1db23
commit dab55f5a6f
No known key found for this signature in database
GPG Key ID: F3614B02ACBE438E
11 changed files with 188 additions and 4 deletions

View File

@ -28,3 +28,13 @@
}
}
}
-e { "configurations": { "launch": {
"adapter": "CodeLLDB",
"filetypes": [ "rust" ],
"configuration": {
"request": "launch",
"program": "${workspaceRoot}/target/debug/HERE"
}
}
}
}

View File

@ -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);
}

View File

@ -0,0 +1,10 @@
{ "configurations": { "launch": {
"adapter": "CodeLLDB",
"filetypes": [ "rust" ],
"configuration": {
"request": "launch",
"program": "${workspaceRoot}/target/debug/matchoptions"
}
}
}
}

7
chapter6/matchoptions/Cargo.lock generated Normal file
View File

@ -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"

View File

@ -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]

View File

@ -0,0 +1,13 @@
fn plus_one(x: Option<i32>) -> Option<i32> {
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);
}

View File

@ -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]

View File

@ -0,0 +1,3 @@
fn main() {
println!("Hello, world!");
}

7
chapter7/restaurant/Cargo.lock generated Normal file
View File

@ -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"

View File

@ -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]

View File

@ -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;
;
}