From eb9773627e94a0db1b8d5c362c14e3bfc77ca2a3 Mon Sep 17 00:00:00 2001 From: Jasper Ras Date: Thu, 21 Aug 2025 21:48:08 +0200 Subject: [PATCH] some more stuff --- ch4/slices/Cargo.lock | 7 +++ ch4/slices/Cargo.toml | 6 +++ ch4/slices/src/main.rs | 18 +++++++ ch5/rectangles/Cargo.lock | 7 +++ ch5/rectangles/Cargo.toml | 6 +++ ch5/rectangles/src/main.rs | 37 +++++++++++++++ ch7/restaurant/Cargo.lock | 7 +++ ch7/restaurant/Cargo.toml | 6 +++ ch7/restaurant/src/front_of_house.rs | 2 + ch7/restaurant/src/front_of_house/hosting.rs | 2 + ch7/restaurant/src/front_of_house/serving.rs | 3 ++ ch7/restaurant/src/lib.rs | 49 ++++++++++++++++++++ 12 files changed, 150 insertions(+) create mode 100644 ch4/slices/Cargo.lock create mode 100644 ch4/slices/Cargo.toml create mode 100644 ch4/slices/src/main.rs create mode 100644 ch5/rectangles/Cargo.lock create mode 100644 ch5/rectangles/Cargo.toml create mode 100644 ch5/rectangles/src/main.rs create mode 100644 ch7/restaurant/Cargo.lock create mode 100644 ch7/restaurant/Cargo.toml create mode 100644 ch7/restaurant/src/front_of_house.rs create mode 100644 ch7/restaurant/src/front_of_house/hosting.rs create mode 100644 ch7/restaurant/src/front_of_house/serving.rs create mode 100644 ch7/restaurant/src/lib.rs diff --git a/ch4/slices/Cargo.lock b/ch4/slices/Cargo.lock new file mode 100644 index 0000000..5d47168 --- /dev/null +++ b/ch4/slices/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 4 + +[[package]] +name = "slices" +version = "0.1.0" diff --git a/ch4/slices/Cargo.toml b/ch4/slices/Cargo.toml new file mode 100644 index 0000000..4ad77ed --- /dev/null +++ b/ch4/slices/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "slices" +version = "0.1.0" +edition = "2024" + +[dependencies] diff --git a/ch4/slices/src/main.rs b/ch4/slices/src/main.rs new file mode 100644 index 0000000..9bda1ef --- /dev/null +++ b/ch4/slices/src/main.rs @@ -0,0 +1,18 @@ +fn first_word(s: &String) -> usize { + let bytes = s.as_bytes(); + + for (i, &item) in bytes.iter().enumerate() { + if item == b' ' { + return i; + } + } + + s.len() +} + +fn main() { + let mut s = String::from("hello world"); + let word = first_word(&s); + println!("{}", word); + s.clear(); +} diff --git a/ch5/rectangles/Cargo.lock b/ch5/rectangles/Cargo.lock new file mode 100644 index 0000000..d888807 --- /dev/null +++ b/ch5/rectangles/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 4 + +[[package]] +name = "rectangles" +version = "0.1.0" diff --git a/ch5/rectangles/Cargo.toml b/ch5/rectangles/Cargo.toml new file mode 100644 index 0000000..bd5e247 --- /dev/null +++ b/ch5/rectangles/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "rectangles" +version = "0.1.0" +edition = "2024" + +[dependencies] diff --git a/ch5/rectangles/src/main.rs b/ch5/rectangles/src/main.rs new file mode 100644 index 0000000..ec7a231 --- /dev/null +++ b/ch5/rectangles/src/main.rs @@ -0,0 +1,37 @@ +#[derive(Debug)] +struct Rect { + width: u32, + height: u32, +} + +impl Rect { + fn area(&self) -> u32 { + self.width * self.height + } + + fn can_hold(&self, other: &Rect) -> bool { + self.width >= other.width && self.height >= other.height + } +} + +fn main() { + let rect1 = Rect { + width: 30, + height: 50, + }; + + let rect2 = Rect { + width: 29, + height: 50, + }; + + let rect3 = Rect { + width: 29, + height: 51, + }; + + // println!("The area is {rect1:#?}"); + println!("{} * {} = {}", rect1.width, rect1.height, rect1.area()); + println!("Can rect1 hold rect2? {}", rect1.can_hold(&rect2)); + println!("Can rect1 hold rect3? {}", rect1.can_hold(&rect3)); +} diff --git a/ch7/restaurant/Cargo.lock b/ch7/restaurant/Cargo.lock new file mode 100644 index 0000000..73e0f82 --- /dev/null +++ b/ch7/restaurant/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 4 + +[[package]] +name = "restaurant" +version = "0.1.0" diff --git a/ch7/restaurant/Cargo.toml b/ch7/restaurant/Cargo.toml new file mode 100644 index 0000000..8e47b7a --- /dev/null +++ b/ch7/restaurant/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "restaurant" +version = "0.1.0" +edition = "2024" + +[dependencies] diff --git a/ch7/restaurant/src/front_of_house.rs b/ch7/restaurant/src/front_of_house.rs new file mode 100644 index 0000000..c6cb0c1 --- /dev/null +++ b/ch7/restaurant/src/front_of_house.rs @@ -0,0 +1,2 @@ +pub mod hosting; +mod serving; diff --git a/ch7/restaurant/src/front_of_house/hosting.rs b/ch7/restaurant/src/front_of_house/hosting.rs new file mode 100644 index 0000000..da0f891 --- /dev/null +++ b/ch7/restaurant/src/front_of_house/hosting.rs @@ -0,0 +1,2 @@ +pub fn add_to_waitlist() {} +fn seat_at_table() {} diff --git a/ch7/restaurant/src/front_of_house/serving.rs b/ch7/restaurant/src/front_of_house/serving.rs new file mode 100644 index 0000000..3829b04 --- /dev/null +++ b/ch7/restaurant/src/front_of_house/serving.rs @@ -0,0 +1,3 @@ +fn take_order() {} +fn serve_order() {} +fn take_payment() {} diff --git a/ch7/restaurant/src/lib.rs b/ch7/restaurant/src/lib.rs new file mode 100644 index 0000000..cdb1e3f --- /dev/null +++ b/ch7/restaurant/src/lib.rs @@ -0,0 +1,49 @@ +mod front_of_house; + +mod back_of_house { + // All fields are public or private along with the enum itself + pub enum Appetizer { + Soup, + Salad, + } + + // Fields have to be marked pub as well to be used publicly + pub struct Breakfast { + pub toast: String, + seasonal_fruit: String, + } + + 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(); // Child scope can access anything in parent + } + + fn cook_order() {} +} + +fn deliver_order() { } + +// Note how the front_of_house mod is still private. +// eat_at_restaurant can access its public children because it is +// defined at the same scope. +pub fn eat_at_restaurant() { + // abs path + crate::front_of_house::hosting::add_to_waitlist(); + + // rel path + front_of_house::hosting::add_to_waitlist(); + + let mut meal = back_of_house::Breakfast::summer("Rye"); + meal.toast = String::from("Wheat"); + println!("id like {} toast please", meal.toast); + // meal.seasonal_fruit = String::from("blackberries"); +}