Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Discussion/07 Managing Growing Projects/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
target/
7 changes: 7 additions & 0 deletions Discussion/07 Managing Growing Projects/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions Discussion/07 Managing Growing Projects/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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]
25 changes: 25 additions & 0 deletions Discussion/07 Managing Growing Projects/src/bin/ch07-03/main.rs
Original file line number Diff line number Diff line change
@@ -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();
}
18 changes: 18 additions & 0 deletions Discussion/07 Managing Growing Projects/src/bin/ch07-08/main.rs
Original file line number Diff line number Diff line change
@@ -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();
}
44 changes: 44 additions & 0 deletions Discussion/07 Managing Growing Projects/src/bin/ch07-09/main.rs
Original file line number Diff line number Diff line change
@@ -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
);
}
28 changes: 28 additions & 0 deletions Discussion/07 Managing Growing Projects/src/bin/ch07-12/main.rs
Original file line number Diff line number Diff line change
@@ -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();
}
25 changes: 25 additions & 0 deletions Discussion/07 Managing Growing Projects/src/bin/ch07-13/main.rs
Original file line number Diff line number Diff line change
@@ -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();
}
26 changes: 26 additions & 0 deletions Discussion/07 Managing Growing Projects/src/bin/ch07-17/main.rs
Original file line number Diff line number Diff line change
@@ -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();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod vegetables;
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#[derive(Debug)]
pub struct Asparagus {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
use crate::garden::vegetables::Asparagus;

pub mod garden;

fn main() {
let plant = Asparagus {};
println!("I'm growing {:?}!", plant);
}
3 changes: 3 additions & 0 deletions Discussion/07 Managing Growing Projects/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fn main() {
println!("Modules are awesome!");
}