diff --git a/Discussion/07 Managing Growing Projects/.gitignore b/Discussion/07 Managing Growing Projects/.gitignore new file mode 100644 index 0000000..2f7896d --- /dev/null +++ b/Discussion/07 Managing Growing Projects/.gitignore @@ -0,0 +1 @@ +target/ diff --git a/Discussion/07 Managing Growing Projects/Cargo.lock b/Discussion/07 Managing Growing Projects/Cargo.lock new file mode 100644 index 0000000..27cf8bd --- /dev/null +++ b/Discussion/07 Managing Growing Projects/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "study-rust-ch07" +version = "0.1.0" diff --git a/Discussion/07 Managing Growing Projects/Cargo.toml b/Discussion/07 Managing Growing Projects/Cargo.toml new file mode 100644 index 0000000..1563ffd --- /dev/null +++ b/Discussion/07 Managing Growing Projects/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "study-rust-ch07" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/Discussion/07 Managing Growing Projects/src/bin/ch07-03/main.rs b/Discussion/07 Managing Growing Projects/src/bin/ch07-03/main.rs new file mode 100644 index 0000000..e77ddf2 --- /dev/null +++ b/Discussion/07 Managing Growing Projects/src/bin/ch07-03/main.rs @@ -0,0 +1,25 @@ +/* Chapter 07, Listing 7-3 + * 모듈과 그 구성 요소들은 private이 기본값 + * 한 모듈 안에 속한 변수/함수/모듈들끼리는 private이어도 참조할 수 있지만 + * 변수/함수/모듈이 private이면 해당 구성요소가 속한 모듈 밖에서 참조할 수 없음 + * 참조하고 싶으면 `pub` 키워드를 사용해 해당 구성요소를 public으로 선언해야 함 + */ +mod front_of_house { + mod hosting { + // pub mod hosting { + fn add_to_waitlist() {} + // pub fn add_to_waitlist() {} + } +} + +pub fn eat_at_restaurant() { + // Absolute path (does not work) + crate::front_of_house::hosting::add_to_waitlist(); + + // Relative path (does not work) + front_of_house::hosting::add_to_waitlist(); +} + +fn main() { + eat_at_restaurant(); +} diff --git a/Discussion/07 Managing Growing Projects/src/bin/ch07-08/main.rs b/Discussion/07 Managing Growing Projects/src/bin/ch07-08/main.rs new file mode 100644 index 0000000..467716c --- /dev/null +++ b/Discussion/07 Managing Growing Projects/src/bin/ch07-08/main.rs @@ -0,0 +1,18 @@ +/* Reference: Chapter 07, Listing 7-8 + * 모듈 밖에서는 모듈 안을 참조할 수 없지만, 반대는 가능함 + * `super` 키워드를 사용하여 상위 모듈을 참조할 수 있음 + */ +fn deliver_order() {} + +mod back_of_house { + pub fn fix_incorrect_order() { + cook_order(); + super::deliver_order(); + } + + fn cook_order() {} +} + +fn main() { + back_of_house::fix_incorrect_order(); +} diff --git a/Discussion/07 Managing Growing Projects/src/bin/ch07-09/main.rs b/Discussion/07 Managing Growing Projects/src/bin/ch07-09/main.rs new file mode 100644 index 0000000..473d8c7 --- /dev/null +++ b/Discussion/07 Managing Growing Projects/src/bin/ch07-09/main.rs @@ -0,0 +1,44 @@ +/* Reference: Chapter 07, Listing 7-9 + * Struct가 public이더라도, 그 field는 private이 기본값임 + * Enum이 public이면, 그 variant는 항상 public임 + */ +mod back_of_house { + 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"), + } + } + } + + #[derive(Debug)] + pub enum Appetizer { + Soup, + Salad, + } +} + +fn main() { + // Order a breakfast in the summer with Rye toast + let mut meal = back_of_house::Breakfast::summer("Rye"); + // Change our mind about what bread we'd like + meal.toast = String::from("Wheat"); + println!("I'd like {} toast please", meal.toast); + + // The next line won't compile if we uncomment it; we're not allowed + // to see or modify the seasonal fruit that comes with the meal + meal.seasonal_fruit = String::from("blueberries"); + + let order1 = back_of_house::Appetizer::Soup; + let order2 = back_of_house::Appetizer::Salad; + println!( + "I'd like {:?} and {:?} as appetizers, please", + order1, order2 + ); +} diff --git a/Discussion/07 Managing Growing Projects/src/bin/ch07-12/main.rs b/Discussion/07 Managing Growing Projects/src/bin/ch07-12/main.rs new file mode 100644 index 0000000..026105d --- /dev/null +++ b/Discussion/07 Managing Growing Projects/src/bin/ch07-12/main.rs @@ -0,0 +1,28 @@ +/* Reference: Chapter 07, Listing 7-12 + * `use` 키워드를 사용하여 shortcut을 설정할 수 있음 + * 해당 shortcut은 `use`가 사용된 모듈 안에서 사용할 수 있음 + */ +mod front_of_house { + pub mod hosting { + pub fn add_to_waitlist() {} + } +} + +use crate::front_of_house::hosting; + +mod customer { + pub fn eat_at_restaurant() { + // Incorrect + hosting::add_to_waitlist(); + + // Correct + // super::hosting::add_to_waitlist(); + + // Also correct + // super::front_of_house::hosting::add_to_waitlist(); + } +} + +fn main() { + customer::eat_at_restaurant(); +} diff --git a/Discussion/07 Managing Growing Projects/src/bin/ch07-13/main.rs b/Discussion/07 Managing Growing Projects/src/bin/ch07-13/main.rs new file mode 100644 index 0000000..da6c97a --- /dev/null +++ b/Discussion/07 Managing Growing Projects/src/bin/ch07-13/main.rs @@ -0,0 +1,25 @@ +/* Reference: Chapter 07, Listing 7-13 + * `as` 키워드를 사용하여 alias를 만들 수 있음 + */ +mod front_of_house { + pub mod hosting { + pub fn add_to_waitlist() {} + } +} + +use crate::front_of_house::hosting::add_to_waitlist; + +pub fn eat_at_restaurant() { + add_to_waitlist(); +} + +use crate::front_of_house::hosting::add_to_waitlist as my_add_to_waitlist; + +pub fn drink_at_restaurant() { + my_add_to_waitlist(); +} + +fn main() { + eat_at_restaurant(); + drink_at_restaurant(); +} diff --git a/Discussion/07 Managing Growing Projects/src/bin/ch07-17/main.rs b/Discussion/07 Managing Growing Projects/src/bin/ch07-17/main.rs new file mode 100644 index 0000000..7aabeef --- /dev/null +++ b/Discussion/07 Managing Growing Projects/src/bin/ch07-17/main.rs @@ -0,0 +1,26 @@ +/* Reference: Chapter 07, Listing 7-17 + * `pub use` 키워드를 사용하여 shortcut을 re-export할 수 있음 + */ +mod front_of_house { + mod hosting { + pub fn add_to_waitlist() {} + } + pub use hosting::add_to_waitlist; + pub mod counter { + pub fn greet() { + super::add_to_waitlist() + } + } +} + +pub fn eat_at_restaurant() { + // Incorrect since `front_of_house::hosting` is private + // front_of_house::hosting::add_to_waitlist(); + + // Correct + front_of_house::add_to_waitlist(); +} + +fn main() { + front_of_house::counter::greet(); +} diff --git a/Discussion/07 Managing Growing Projects/src/bin/vegetables/garden.rs b/Discussion/07 Managing Growing Projects/src/bin/vegetables/garden.rs new file mode 100644 index 0000000..6c7f9b1 --- /dev/null +++ b/Discussion/07 Managing Growing Projects/src/bin/vegetables/garden.rs @@ -0,0 +1 @@ +pub mod vegetables; diff --git a/Discussion/07 Managing Growing Projects/src/bin/vegetables/garden/vegetables.rs b/Discussion/07 Managing Growing Projects/src/bin/vegetables/garden/vegetables.rs new file mode 100644 index 0000000..b00f785 --- /dev/null +++ b/Discussion/07 Managing Growing Projects/src/bin/vegetables/garden/vegetables.rs @@ -0,0 +1,2 @@ +#[derive(Debug)] +pub struct Asparagus {} diff --git a/Discussion/07 Managing Growing Projects/src/bin/vegetables/main.rs b/Discussion/07 Managing Growing Projects/src/bin/vegetables/main.rs new file mode 100644 index 0000000..7a024a9 --- /dev/null +++ b/Discussion/07 Managing Growing Projects/src/bin/vegetables/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/Discussion/07 Managing Growing Projects/src/main.rs b/Discussion/07 Managing Growing Projects/src/main.rs new file mode 100644 index 0000000..e8bd196 --- /dev/null +++ b/Discussion/07 Managing Growing Projects/src/main.rs @@ -0,0 +1,3 @@ +fn main() { + println!("Modules are awesome!"); +}