some more stuff

This commit is contained in:
Jasper Ras 2025-08-21 21:48:08 +02:00
parent cc2f1b67a8
commit eb9773627e
12 changed files with 150 additions and 0 deletions

7
ch4/slices/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 = 4
[[package]]
name = "slices"
version = "0.1.0"

6
ch4/slices/Cargo.toml Normal file
View file

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

18
ch4/slices/src/main.rs Normal file
View file

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

7
ch5/rectangles/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 = 4
[[package]]
name = "rectangles"
version = "0.1.0"

View file

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

View file

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

7
ch7/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 = 4
[[package]]
name = "restaurant"
version = "0.1.0"

View file

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

View file

@ -0,0 +1,2 @@
pub mod hosting;
mod serving;

View file

@ -0,0 +1,2 @@
pub fn add_to_waitlist() {}
fn seat_at_table() {}

View file

@ -0,0 +1,3 @@
fn take_order() {}
fn serve_order() {}
fn take_payment() {}

49
ch7/restaurant/src/lib.rs Normal file
View file

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