This commit is contained in:
Rowan Torbitzky-Lane 2025-03-21 14:32:00 -05:00
parent b3dfadba39
commit dacb5fa112
9 changed files with 211 additions and 0 deletions

7
ch6/book_match/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 = "book_match"
version = "0.1.0"

View File

@ -0,0 +1,6 @@
[package]
name = "book_match"
version = "0.1.0"
edition = "2021"
[dependencies]

View File

@ -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<i32>) -> Option<i32> {
// 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() {}

7
ch6/defining_an_enum/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 = "defining_an_enum"
version = "0.1.0"

View File

@ -0,0 +1,6 @@
[package]
name = "defining_an_enum"
version = "0.1.0"
edition = "2021"
[dependencies]

View File

@ -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<i32> = None;
}
// takes either kind of IpAddrKind
fn route(ip_kind: IpAddrKind) {}

7
ch6/if_let/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 = "if_let"
version = "0.1.0"

6
ch6/if_let/Cargo.toml Normal file
View File

@ -0,0 +1,6 @@
[package]
name = "if_let"
version = "0.1.0"
edition = "2021"
[dependencies]

18
ch6/if_let/src/main.rs Normal file
View File

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