diff --git a/ch7/backyard/Cargo.lock b/ch7/backyard/Cargo.lock new file mode 100644 index 0000000..4773c20 --- /dev/null +++ b/ch7/backyard/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "backyard" +version = "0.1.0" diff --git a/ch7/backyard/Cargo.toml b/ch7/backyard/Cargo.toml new file mode 100644 index 0000000..6e904ab --- /dev/null +++ b/ch7/backyard/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "backyard" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/ch7/backyard/src/garden.rs b/ch7/backyard/src/garden.rs new file mode 100644 index 0000000..6c7f9b1 --- /dev/null +++ b/ch7/backyard/src/garden.rs @@ -0,0 +1 @@ +pub mod vegetables; diff --git a/ch7/backyard/src/garden/vegetables.rs b/ch7/backyard/src/garden/vegetables.rs new file mode 100644 index 0000000..b00f785 --- /dev/null +++ b/ch7/backyard/src/garden/vegetables.rs @@ -0,0 +1,2 @@ +#[derive(Debug)] +pub struct Asparagus {} diff --git a/ch7/backyard/src/main.rs b/ch7/backyard/src/main.rs new file mode 100644 index 0000000..29dcc82 --- /dev/null +++ b/ch7/backyard/src/main.rs @@ -0,0 +1,8 @@ +use crate::garden::vegetables::Asparagus; + +pub mod garden; + +fn main() { + let plant = Asparagus {}; + println!("I'm growing: {plant:?}!"); +} diff --git a/ch7/restaurant/Cargo.lock b/ch7/restaurant/Cargo.lock new file mode 100644 index 0000000..ba86874 --- /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 = 3 + +[[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..60cec7c --- /dev/null +++ b/ch7/restaurant/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "restaurant" +version = "0.1.0" +edition = "2021" + +[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..a3c3218 --- /dev/null +++ b/ch7/restaurant/src/front_of_house.rs @@ -0,0 +1,7 @@ +pub mod hosting; + +mod serving { + fn take_order() {} + fn serve_order() {} + fn take_payment() {} +} 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/lib.rs b/ch7/restaurant/src/lib.rs new file mode 100644 index 0000000..ca0142f --- /dev/null +++ b/ch7/restaurant/src/lib.rs @@ -0,0 +1,92 @@ +// modules allow control of the privacy of items. +// Code private by default. Can choose to make +// modules and items within them public. +// +// Identifiers in crate paths are followed by :: + +use std::collections::*; +use std::fmt::Result; +use std::io::Result as IoResult; // Can use this glob operator, not recommended tho. Typically only used in testing + +pub fn add(left: u64, right: u64) -> u64 { + left + right +} + +// #[cfg(test)] +// mod tests { +// use super::*; + +// #[test] +// fn it_works() { +// let result = add(2, 2); +// assert_eq!(result, 4); +// } +// } + +mod front_of_house; + +pub fn eat_at_restaurant() { + crate::front_of_house::hosting::add_to_waitlist(); + front_of_house::hosting::add_to_waitlist(); +} + +// starting relative paths with super +fn deliver_order() {} + +mod back_of_house { + fn fix_incorrect_order() { + cook_order(); + super::deliver_order(); + } + + fn cook_order() {} + + // if use pub before struct definition, + // make struct public but fields private + pub struct Breakfast { + pub toast: String, + seasonal_fruit: String, + } + + impl Breakfast { + // because a single field is private, must provide a + // public associated function that constructs an instance + // of Breakfast + pub fn summer(toast: &str) -> Breakfast { + Breakfast { + toast: String::from(toast), + seasonal_fruit: String::from("peaches"), + } + } + } + + // If make enum public, all of its variants are public. + pub enum Appetizer { + Soup, + Salad, + } +} + +pub fn eat_at_restaurant_summer() { + let mut meal = back_of_house::Breakfast::summer("Rye"); + meal.toast = String::from("Wheat"); + println!("I'd like {} toast plz", meal.toast); + + // This line fails because not seasonal_fruit is private + // meal.seasonal_fruit = String::from("blueberries"); +} + +pub fn eat_at_restaurant_appetizer() { + let order1 = back_of_house::Appetizer::Soup; + let order2 = back_of_house::Appetizer::Salad; +} + +// The use statement only applies to the scope that it's in +// use crate::front_of_house::hosting; + +// Can also prepend the above statement with pub to allow for re-exporting +pub use crate::front_of_house::hosting; + +pub fn eat_at_restaurant_in_scope() { + hosting::add_to_waitlist(); +}