some more stuff
This commit is contained in:
parent
cc2f1b67a8
commit
eb9773627e
12 changed files with 150 additions and 0 deletions
7
ch4/slices/Cargo.lock
generated
Normal file
7
ch4/slices/Cargo.lock
generated
Normal 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
6
ch4/slices/Cargo.toml
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
[package]
|
||||
name = "slices"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
18
ch4/slices/src/main.rs
Normal file
18
ch4/slices/src/main.rs
Normal 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
7
ch5/rectangles/Cargo.lock
generated
Normal 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"
|
||||
6
ch5/rectangles/Cargo.toml
Normal file
6
ch5/rectangles/Cargo.toml
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
[package]
|
||||
name = "rectangles"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
37
ch5/rectangles/src/main.rs
Normal file
37
ch5/rectangles/src/main.rs
Normal 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
7
ch7/restaurant/Cargo.lock
generated
Normal 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"
|
||||
6
ch7/restaurant/Cargo.toml
Normal file
6
ch7/restaurant/Cargo.toml
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
[package]
|
||||
name = "restaurant"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
2
ch7/restaurant/src/front_of_house.rs
Normal file
2
ch7/restaurant/src/front_of_house.rs
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
pub mod hosting;
|
||||
mod serving;
|
||||
2
ch7/restaurant/src/front_of_house/hosting.rs
Normal file
2
ch7/restaurant/src/front_of_house/hosting.rs
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
pub fn add_to_waitlist() {}
|
||||
fn seat_at_table() {}
|
||||
3
ch7/restaurant/src/front_of_house/serving.rs
Normal file
3
ch7/restaurant/src/front_of_house/serving.rs
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
fn take_order() {}
|
||||
fn serve_order() {}
|
||||
fn take_payment() {}
|
||||
49
ch7/restaurant/src/lib.rs
Normal file
49
ch7/restaurant/src/lib.rs
Normal 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");
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue