From dacb5fa112d79d9b5e4c56b03a02a56320c5ff90 Mon Sep 17 00:00:00 2001 From: Rowan Torbitzky-Lane Date: Fri, 21 Mar 2025 14:32:00 -0500 Subject: [PATCH] ch6 done --- ch6/book_match/Cargo.lock | 7 +++ ch6/book_match/Cargo.toml | 6 +++ ch6/book_match/src/main.rs | 76 +++++++++++++++++++++++++++++++ ch6/defining_an_enum/Cargo.lock | 7 +++ ch6/defining_an_enum/Cargo.toml | 6 +++ ch6/defining_an_enum/src/main.rs | 78 ++++++++++++++++++++++++++++++++ ch6/if_let/Cargo.lock | 7 +++ ch6/if_let/Cargo.toml | 6 +++ ch6/if_let/src/main.rs | 18 ++++++++ 9 files changed, 211 insertions(+) create mode 100644 ch6/book_match/Cargo.lock create mode 100644 ch6/book_match/Cargo.toml create mode 100644 ch6/book_match/src/main.rs create mode 100644 ch6/defining_an_enum/Cargo.lock create mode 100644 ch6/defining_an_enum/Cargo.toml create mode 100644 ch6/defining_an_enum/src/main.rs create mode 100644 ch6/if_let/Cargo.lock create mode 100644 ch6/if_let/Cargo.toml create mode 100644 ch6/if_let/src/main.rs diff --git a/ch6/book_match/Cargo.lock b/ch6/book_match/Cargo.lock new file mode 100644 index 0000000..b0d96bd --- /dev/null +++ b/ch6/book_match/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "book_match" +version = "0.1.0" diff --git a/ch6/book_match/Cargo.toml b/ch6/book_match/Cargo.toml new file mode 100644 index 0000000..da00df3 --- /dev/null +++ b/ch6/book_match/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "book_match" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/ch6/book_match/src/main.rs b/ch6/book_match/src/main.rs new file mode 100644 index 0000000..ca80611 --- /dev/null +++ b/ch6/book_match/src/main.rs @@ -0,0 +1,76 @@ +enum Coin { + Penny, + Nickel, + Dime, + Quarter, +} + +#[derive(Debug)] +enum UsState { + California, + Alabama, + Missouri, + // so on and so forth +} + +enum StateCoin { + Penny, + Nickel, + Dime, + Quarter(UsState), +} + +fn main() { + let five = Some(5); + let six = plus_one(five); + let none = plus_one(None); + + let dice_roll = 9; + match dice_roll { + 3 => add_fancy_hat(), + 7 => remove_fancy_hat(), + _ => (), // just like Haskell! Can tell Rust that no code should be ran with () + } +} + +fn value_in_cents(coin: Coin) -> u8 { + match coin { + Coin::Penny => { + // can use curly brackets :) + println!("Lucky Penny"); + 1 + } + Coin::Nickel => 5, + Coin::Dime => 10, + Coin::Quarter => 25, + } +} + +fn value_in_cents_state(coin: StateCoin) -> u8 { + match coin { + StateCoin::Penny => { + // can use curly brackets :) + println!("Lucky Penny"); + 1 + } + StateCoin::Nickel => 5, + StateCoin::Dime => 10, + StateCoin::Quarter(state) => { + println!("State quarter from {state:?}!"); + 25 + } + } +} + +fn plus_one(x: Option) -> Option { + // match arms must cover all possibilities + match x { + None => None, + Some(i) => Some(i + 1), + } +} + +fn add_fancy_hat() {} +fn remove_fancy_hat() {} +fn move_player(num_spaces: u8) {} +fn reroll() {} diff --git a/ch6/defining_an_enum/Cargo.lock b/ch6/defining_an_enum/Cargo.lock new file mode 100644 index 0000000..14d89e2 --- /dev/null +++ b/ch6/defining_an_enum/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "defining_an_enum" +version = "0.1.0" diff --git a/ch6/defining_an_enum/Cargo.toml b/ch6/defining_an_enum/Cargo.toml new file mode 100644 index 0000000..2194c99 --- /dev/null +++ b/ch6/defining_an_enum/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "defining_an_enum" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/ch6/defining_an_enum/src/main.rs b/ch6/defining_an_enum/src/main.rs new file mode 100644 index 0000000..c84da0e --- /dev/null +++ b/ch6/defining_an_enum/src/main.rs @@ -0,0 +1,78 @@ +// I could make an emum for a buy/sell/hold signal in a long-only strategy +// cool project from the android team at google: +// https://google.github.io/comprehensive-rust/ + +enum IpAddrKind { + V4, + V6, +} + +struct IpAddrOld { + kind: IpAddrKind, + address: String, +} + +// A better way of representing IP in this case +enum IpAddrFunc { + V4(String), + V6(String), +} + +// Even better way as these enums can hold +// different values, like a tuple, and like Haskell +enum IpAddr { + V4(u8, u8, u8, u8), + V6(String), +} + +enum Message { + Quit, + Move { x: i32, y: i32 }, + Write(String), + ChangeColor(i32, i32, i32), +} + +impl Message { + fn call(&self) { + // method body here + } +} + +fn main() { + let four = IpAddrKind::V4; + let six = IpAddrKind::V6; + + route(four); + route(six); + + let home_old: IpAddrOld = IpAddrOld { + kind: IpAddrKind::V4, + address: String::from("127.0.0.1"), + }; + + let loopback_old = IpAddrOld { + kind: IpAddrKind::V6, + address: String::from("::1"), + }; + + let home = IpAddrFunc::V4(String::from("127.0.0.1")); + let loopback = IpAddrFunc::V6(String::from("::1")); + + let home_num = IpAddr::V4(127, 0, 0, 1); + + // Message section + + let m = Message::Write(String::from("hello")); + m.call(); + + // ------------------------------------------------------------------------------ + // Option enum section + // Essentially Haskell's Maybe Monad + // Rust even has a Prelude :pog: + let some_number = Some(5); + let some_char = Some('e'); + let absent_number: Option = None; +} + +// takes either kind of IpAddrKind +fn route(ip_kind: IpAddrKind) {} diff --git a/ch6/if_let/Cargo.lock b/ch6/if_let/Cargo.lock new file mode 100644 index 0000000..384e67f --- /dev/null +++ b/ch6/if_let/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "if_let" +version = "0.1.0" diff --git a/ch6/if_let/Cargo.toml b/ch6/if_let/Cargo.toml new file mode 100644 index 0000000..7304b2d --- /dev/null +++ b/ch6/if_let/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "if_let" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/ch6/if_let/src/main.rs b/ch6/if_let/src/main.rs new file mode 100644 index 0000000..d1bb61e --- /dev/null +++ b/ch6/if_let/src/main.rs @@ -0,0 +1,18 @@ +// More verboseness + +fn main() { + let config_max = Some(3u8); + if let Some(max) = config_max { + println!("Max configured to be: {max}"); + } + // else would be what goes in the _ +} + +// fn make_sep(usr_str: &str) -> &str { +// if usr_str == "" { +// let default = "=".repeat(10); +// &default +// } else { +// usr_str +// } +// }