7.0 ํŒจํ‚ค์ง€, ํฌ๋ž˜์ดํŠธ, ๋ชจ๋“ˆ์„ ์ด์šฉํ•ด ์ปค์ง€๋Š” ํ”„๋กœ์ ํŠธ๋ฅผ ๊ด€๋ฆฌํ•˜๊ธฐ

  • ํ”„๋กœ์ ํŠธ๊ฐ€ ์ปค์ง€๋ฉด์„œ ์ฝ”๋“œ๋ฅผ ๊ด€๋ฆฌํ•˜๋Š” ๊ฒƒ์ด ์ค‘์š”ํ•ด์ง„๋‹ค, ๊ธฐ๋Šฅ์„ ๋‹จ์œ„๋กœ ๋ชจ๋“ˆํ™”ํ•˜๊ณ  ๋‚˜๋ˆ ์•ผ ๊ด€๋ฆฌํ•˜๋Š”๊ฒƒ์ด ํŽธ๋ฆฌํ•˜๋‹ค.

  • ์ด ์ฑ•ํ„ฐ๋Š” ๊ทธ๋Ÿฌํ•œ ๊ฒƒ๋“ค์„ ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ๋‹ค๋ฃฌ๋‹ค.

  • ๋†’์€ ๋ ˆ๋ฒจ์—์„œ์˜ ์ฝ”๋“œ ์žฌ์‚ฌ์šฉ์„ฑ์„ ์œ„ํ•ด encaptulating, implementing๋“ฑ ๋‹ฌ์„ฑํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ๋‹ค๋ฃฌ๋‹ค.

  • ์‹ค์ œ ๊ตฌํ˜„์„ ์•Œ์ง€ ๋ชปํ•ด๋„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ์ธํ„ฐํŽ˜์ด์Šค๋กœ ์ถ”์ƒํ™”๋ฅผ ์ œ๊ณตํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ๋‹ค๋ฃฌ๋‹ค.

  • scope์™€ namespace๋ฅผ ์ด์šฉํ•ด ์ฝ”๋“œ๋ฅผ ๊ตฌ์กฐํ™”ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ๋‹ค๋ฃฌ๋‹ค.

  • ์œ„์— ๋‚ด์šฉ๋“ค์„ ์•„์šฐ๋ฅด๋Š” ๋Ÿฌ์ŠคํŠธ์˜ ๋ชจ๋“ˆํ™” ์‹œ์Šคํ…œ์€ ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

    • package : ํฌ๋ ˆ์ดํŠธ๋ฅผ ๋นŒ๋“œํ•˜๊ณ  ๊ณต์œ ํ•˜๋Š” ๋‹จ์œ„
    • crate : ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋‚˜ ์‹คํ–‰ํŒŒ์ผ์„ ๋นŒ๋“œํ•˜๋Š” ๋‹จ์œ„
    • module : ์ฝ”๋“œ๋ฅผ ๊ทธ๋ฃนํ™”ํ•˜๊ณ  namespace๋ฅผ ์ œ๊ณตํ•˜๋Š” ๋‹จ์œ„
    • path : ๋ชจ๋“ˆ์„ ์ฐธ์กฐํ•˜๋Š” ๋ฐฉ๋ฒ•

7.1 Package and Crates

  • crate๋Š” ๋Ÿฌ์ŠคํŠธ์˜ ์ปดํŒŒ์ผ๋Ÿฌ๊ฐ€ ํ•œ๋ฒˆ์— ๊ณ ๋ ค ํ•  ์ˆ˜ ์žˆ๋Š” ๊ฐ€์žฅ ์ตœ์†Œ์˜ ์ฝ”๋“œ ๋‹จ์œ„์ด๋‹ค.

  • cargo ๊ฐ€ ์•„๋‹Œ rustc๋กœ ์ปดํŒŒ์ผ์„ ํ•˜๋Š” ๊ฒฝ์šฐ์—, ํ•˜๋‚˜์˜ ์†Œ์Šค์ฝ”๋“œํŒŒ์ผ๋งŒ ์ปดํŒŒ์ผ ํ•˜๋”๋ผ๋„, ์ปดํŒŒ์ผ๋Ÿฌ๋Š” ๊ทธ ํ•˜๋‚˜์˜ ํŒŒ์ผ์„ ํ•˜๋‚˜์˜ ํฌ๋ ˆ์ดํŠธ๋กœ ์ทจ๊ธ‰ํ•œ๋‹ค.

  • crate๋Š” ๋ชจ๋“ˆ์„ ํฌํ•จ ํ•  ์ˆ˜ ์žˆ๋Š”๋ฐ, ํ•ด๋‹น ๋ชจ๋“ˆ์€ crate๋กœ ์ปดํŒŒ์ผ๋œ ๋‹ค๋ฅธ ๋ชจ๋“ˆ์— ์ •์˜ ๋œ ๊ฒƒ์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

  • crate๋Š” ๋‘๊ฐ€์ง€ ํ˜•ํƒœ๋กœ ๋‚˜๋‰œ๋‹ค.

    • binary crate : ์‹คํ–‰ํŒŒ์ผ์„ ๋งŒ๋“œ๋Š” ํฌ๋ ˆ์ดํŠธ, cli ํ”„๋กœ๊ทธ๋žจ์ด๋‚˜ ์„œ๋ฒ„ ํ”„๋กœ๊ทธ๋žจ์ฒ˜๋Ÿผ, ๋˜ํ•œ mainํ•จ์ˆ˜๋ฅผ ๊ผญ ใ…Žํฌํ•จํ•ด์•ผํ•œ๋‹ค.
    • library crate : ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ๋งŒ๋“œ๋Š” ํฌ๋ ˆ์ดํŠธ, mainํ•จ์ˆ˜๋ฅผ ํฌํ•จํ•˜์ง€ ์•Š์œผ๋ฉฐ, ์‹คํ–‰ ๊ฐ€๋Šฅํ•œ ๋ฐ”์ด๋„ˆ๋ฆฌ๋ฅผ ๋งŒ๋“ค์ง€ ์•Š๋Š”๋‹ค.
  • crate root๋Š” ์ปดํŒŒ์ผ๋Ÿฌ๊ฐ€ ํฌ๋ ˆ์ดํŠธ๋ฅผ ์‹œ์ž‘ํ•˜๋Š” ํŒŒ์ผ์„ ๊ฐ€๋ฆฌํ‚จ๋‹ค. ์ด ํŒŒ์ผ์€ crate์˜ ๋ฃจํŠธ ๋ชจ๋“ˆ์„ ์ •์˜ํ•œ๋‹ค.

  • package๋Š” ํ•˜๋‚˜ ์ด์ƒ์˜ ํฌ๋ ˆ์ดํŠธ๋“ค์„ ํฌํ•จํ•˜๋Š”(bundle) ๋‹จ์œ„์ด๋‹ค.

  • package๋Š” Cargo.tomlํŒŒ์ผ์„ ๊ฐ€์ง€๋ฉฐ, ์ด ํŒŒ์ผ์€ package์˜ ๋ฉ”ํƒ€๋ฐ์ดํ„ฐ๋ฅผ ์ •์˜ํ•ด์„œ package ๋‚ด๋ถ€์˜ ํ•ด๋‹น ํฌ๋ ˆ์ดํŠธ๋“ค์„ ์–ด๋–ป๊ฒŒ ๋นŒ๋“œ ํ•ด์•ผ ํ•˜๋Š”์ง€ ์•Œ๋ ค์ค€๋‹ค.

  • package๋Š” ํ•˜๋‚˜์˜ ํฌ๋ ˆ์ดํŠธ๋งŒ ํฌํ•จ ํ•  ์ˆ˜๋„ ์žˆ๊ณ , ์—ฌ๋Ÿฌ๊ฐœ์˜ ํฌ๋ ˆ์ดํŠธ๋ฅผ ํฌํ•จ ํ•  ์ˆ˜๋„ ์žˆ๋‹ค.

  • ๋‹ค์ˆ˜์˜ binary crate๋ฅผ ๊ฐ€์งˆ ์ˆ˜ ์žˆ์œผ๋‚˜, ํ•˜๋‚˜์˜ library crate๋งŒ ๊ฐ€์งˆ ์ˆ˜ ์žˆ๋‹ค.

  • src/main.rs์™€ src/lib.rs๋Š” ๊ฐ๊ฐ binary crate์™€ library crate์˜ crate root์ด๋‹ค.

7.2 Defining Modules to Control Scope and Privacy

์ด ์ฑ•ํ„ฐ์—์„œ๋Š” ๋ชจ๋“ˆ๊ณผ ๋ชจ๋“ˆ ์‹œ์Šคํ…œ์— ๋Œ€ํ•ด ๋‹ค๋ฃฌ๋‹ค. ์ฃผ์š” ํ‚ค์›Œ๋“œ๋“ค์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค

  • module : ์ฝ”๋“œ๋ฅผ ๊ทธ๋ฃนํ™”ํ•˜๊ณ  namespace๋ฅผ ์ œ๊ณตํ•˜๋Š” ๋‹จ์œ„
  • use : ๋ชจ๋“ˆ์„ ๋‹ค๋ฅธ ์Šค์ฝ”ํ”„๋กœ ๊ฐ€์ ธ์˜ค๋Š” ํ‚ค์›Œ๋“œ
  • as : ๊ฐ€์ ธ์˜จ ๋ชจ๋“ˆ์„ ๋‹ค๋ฅธ ์ด๋ฆ„์œผ๋กœ ์‚ฌ์šฉํ•˜๋Š” ํ‚ค์›Œ๋“œ
  • pub : ๋ชจ๋“ˆ์„ ๊ณต๊ฐœํ•˜๋Š” ํ‚ค์›Œ๋“œ

7.2.1 Modules Cheetsheet

๋Ÿฌ์ŠคํŠธ์˜ ๋ชจ๋“ˆ ์‹œ์Šคํ…œ์ด ๋”ฐ๋ฅด๋Š” ๊ทœ์น™๊ณผ ๋™์ž‘ํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

  • Start from the crate root : ํฌ๋ ˆ์ดํŠธ๋ฅผ ์ปดํŒŒ์ผ ํ•  ๋•Œ, ์ปดํŒŒ์ผ๋Ÿฌ๋Š” ์ปดํŒŒ์ผ ํ•  ์ฝ”๋“œ๋ฅผ ์ฐพ๊ธฐ ์œ„ํ•ด crate root๋ฅผ ์ฐพ๋Š”๋‹ค. (src/main.rs์™€ src/lib.rs).

  • Declaring modules : crate root์—์„œ ์ƒˆ๋กœ์šด ๋ชจ๋“ˆ๋“ค์„ ์„ ์–ธ ํ•  ์ˆ˜ ์žˆ๋”ฐ. ๋ชจ๋“ˆ์€ mod ํ‚ค์›Œ๋“œ์™€ ์„ ์–ธํ•œ๋‹ค. ์˜ˆ๋ฅผ๋“ค์–ด mod garden;๊ณผ ๊ฐ™์ด ์„ ์–ธํ•˜๋ฉด ์ปดํŒŒ์ผ๋Ÿฌ๋Š” ๋ชจ๋“ˆ์˜ ์ฝ”๋“œ๋ฅผ ์•„๋ž˜์™€ ๊ฐ™์€ ์œ„์น˜์—์„œ ์ฐพ๋Š”๋‹ค

  • Defining modules in other files : ๋ชจ๋“ˆ์„ ๋‹ค๋ฅธ ํŒŒ์ผ์— ์ •์˜ ํ•  ์ˆ˜ ์žˆ๋‹ค. ์ด๋•Œ ํŒŒ์ผ์˜ ์ด๋ฆ„์€ ๋ชจ๋“ˆ์˜ ์ด๋ฆ„๊ณผ ๊ฐ™์•„์•ผ ํ•œ๋‹ค. ์˜ˆ๋ฅผ๋“ค์–ด mod garden;์ด๋ผ๊ณ  ์„ ์–ธํ•˜๋ฉด, garden.rs๋‚˜ garden/mod.rsํŒŒ์ผ์„ ์ฐพ๋Š”๋‹ค.

    • Inline : mod garden { ... } ์™€ ๊ฐ™์ด ์ค‘๊ด„ํ˜ธ์™€ ์„ธ๋ฏธ์ฝœ๋ก ์œผ๋กœ ์ •์˜๋œ ๋ชจ๋“ˆ์ด ์žˆ๋Š”์ง€ ์ธ๋ผ์ธ์—์„œ ์ฐพ๋Š”๋‹ค.
    • File : src/garden.rs๋‚˜ src/garden/mod.rsํŒŒ์ผ์„ ์ฐพ๋Š”๋‹ค.
  • Declaring subomodules : ๋ชจ๋“ˆ ๋‚ด๋ถ€์— ๋˜ ๋‹ค๋ฅธ ๋ชจ๋“ˆ์„ ์„ ์–ธ ํ•  ์ˆ˜ ์žˆ๋‹ค(crate root์—๋Š” ์•ˆ๋œ๋‹ค). ์ด๋•Œ๋„ mod ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค. ์ด๋ฒˆ์—๋Š” mod vegetables;๋ฅผ mod garden;์•ˆ์— ์„ ์–ธํ•˜๋ฉด ์•„๋ž˜์™€ ๊ฐ™์€ ์ˆœ์„œ๋กœ ์ฐพ๋Š”๋‹ค.

  • Inline : mod vegetables { ... } ์™€ ๊ฐ™์ด ์ค‘๊ด„ํ˜ธ์™€ ์„ธ๋ฏธ์ฝœ๋ก ์œผ๋กœ ์ •์˜๋œ ๋ชจ๋“ˆ์ด ์žˆ๋Š”์ง€ ์ธ๋ผ์ธ์—์„œ ์ฐพ๋Š”๋‹ค.

  • File : src/garden/vegetables.rs๋‚˜ src/garden/vegetables/mod.rsํŒŒ์ผ์„ ์ฐพ๋Š”๋‹ค.

  • Paths to code in modules : ๋ชจ๋“ˆ์ด ํฌ๋ ˆ์ดํŠธ์— ์ผ๋ถ€๊ฐ€ ๋œ๋‹ค๋ฉด, ํ•ด๋‹น ๋ชจ๋“ˆ์˜ ์ฝ”๋“œ๋ฅผ ํฌ๋ ˆ์ดํŠธ์˜ ์–ด๋””์—์„œ๋“  ์ฐธ์กฐํ•  ์ˆ˜ ์žˆ๋‹ค. ๋งŒ์•ฝ pricacy ๊ทœ์น™์— ์–ด๊ธ‹๋‚˜์ง€ ์•Š๋Š”๋‹ค๋ฉด Asparagus๋ชจ๋“ˆ์˜ type์„ ํ˜ธ์ถœํ•˜๋ ค๋ฉด crate::garden::vegetables::Asparagus์™€ ๊ฐ™์ด ํ˜ธ์ถœ ํ•  ์ˆ˜ ์žˆ๋‹ค.

  • Private vs public : ๋ชจ๋“ˆ ๋‚ด๋ถ€์˜ ์ฝ”๋“œ๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ private์ด๋‹ค. pub ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด ๋ชจ๋“ˆ์„ ๊ณต๊ฐœ ํ•  ์ˆ˜ ์žˆ๋‹ค. ๋ชจ๋“ˆ์„ ๊ณต๊ฐœํ•˜๋ฉด, ๋‹ค๋ฅธ ํฌ๋ ˆ์ดํŠธ์—์„œ ํ•ด๋‹น ๋ชจ๋“ˆ์„ ์‚ฌ์šฉ ํ•  ์ˆ˜ ์žˆ๋‹ค.

  • Use : use ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด ๋ชจ๋“ˆ์„ ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์žˆ๋‹ค. use ํ‚ค์›Œ๋“œ๋Š” ๋ชจ๋“ˆ์„ ํ˜„์žฌ ์Šค์ฝ”ํ”„๋กœ ๊ฐ€์ ธ์˜จ๋‹ค.

// binary crate name backyard
backyard
โ”œโ”€โ”€ Cargo.lock
โ”œโ”€โ”€ Cargo.toml
โ””โ”€โ”€ src
    โ”œโ”€โ”€ garden
    โ”‚ย ย  โ””โ”€โ”€ vegetables.rs
    โ”œโ”€โ”€ garden.rs
    โ””โ”€โ”€ main.rs

File : src/main.rs

use crate::garden::vegetables::Asparagus;

pub mod garden;

fn main() {
    let plant = Asparagus {};
    println!("I'm growing {:?}!", plant);
}

File : src/garden.rs

pub mod vegetables;

File : src/garden/vegetables.rs

pub struct Asparagus {
    pub name: String,
}
  • ๋ชจ๋“ˆ์€ ํฌ๋ ˆ์ดํŠธ ๋‚ด๋ถ€์˜ ์ฝ”๋“œ๋ฅผ ์กฐ์งํ™”ํ–‡ํ•ด์„œ ์ฝ๊ธฐ ์‰ฝ๊ณ  ์žฌ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•œ ์ฝ”๋“œ๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์žˆ๊ฒŒ ํ•ด์ค€๋‹ค.

  • ๋˜ํ•œ private๊ณผ public์œผ๋กœ ์ฝ”๋“œ์˜ ์ ‘๊ทผ์„ฑ์„ ์ œ์–ดํ•  ์ˆ˜ ์žˆ๋‹ค.

  • ๊ธฐ๋ณธ์ ์œผ๋กœ private์œผ๋กœ ์บก์Šํ™” ํ•˜๊ธฐ ๋•Œ๋ฌธ์—, ๋‹ค๋ฅธ ์ฝ”๋“œ์—์„œ ํ•ด๋‹น ๊ตฌํ˜„์„ ์•Œ๊ฑฐ๋‚˜ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†๋‹ค.

  • ์‹ค์ œ๋กœ ์‚ฌ์šฉํ•˜๊ฑฐ๋‚˜ ์˜์กดํ•ด์•ผ ํ•  ์ฝ”๋“œ๋“ค๋งŒ public์œผ๋กœ ๋งŒ๋“ค๋ฉด ๋œ๋‹ค.

  • ์•„๋ž˜๋Š” ๋ชจ๋“ˆ์„ ์‚ฌ์šฉํ•ด ๋ ˆ์Šคํ† ๋ž‘์„ ๋งŒ๋“œ๋Š” ์˜ˆ์ œ์ด๋‹ค.

Filename : src/lib.rs

mod front_of_house {
    mod hosting {
        fn add_to_waitlist() {}

        fn seat_at_table() {}
    }

    mod serving {
        fn take_order() {}

        fn serve_order() {}

        fn take_payment() {}
    }
}
  • ์ด๋Ÿฐ์‹์œผ๋กœ ๋ชจ๋“ˆ์„ ์‚ฌ์šฉํ•˜๊ณ , ๊ทธ๋ฃนํ™”ํ•ด๋‘๋ฉด ์™œ ๊ด€๋ จ๋˜์–ด ์žˆ๋Š”์ง€๋ฅผ ์•Œ ์ˆ˜ ์žˆ๊ณ , ๊ทธ๋Ÿฌํ•œ ์ฝ”๋“œ๋“ค์„ ์‰ฝ๊ฒŒ ์ฐพ์„ ์ˆ˜ ์žˆ๋‹ค.

  • ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ์ด ๋ชจ๋“ˆ์— ์ฝ”๋“œ๋ฅผ ์ถ”๊ฐ€/์ˆ˜์ • ํ•  ๋•Œ๋„, ๊ทธ๋Ÿฌ๊ธฐ ์œ„ํ•œ ๊ฐ€์ด๋“œ๋ผ์ธ์„ ์ œ๊ณตํ•ด ์ค„ ์ˆ˜ ์žˆ๋‹ค.

module tree

crate
 โ””โ”€โ”€ front_of_house
     โ”œโ”€โ”€ hosting
     โ”‚   โ”œโ”€โ”€ add_to_waitlist
     โ”‚   โ””โ”€โ”€ seat_at_table
     โ””โ”€โ”€ serving
         โ”œโ”€โ”€ take_order
         โ”œโ”€โ”€ serve_order
         โ””โ”€โ”€ take_payment
  • ์ฐธ๊ณ ๋กœ src/main.rs๋‚˜ src/lib.rs๋Š” crate root์ธ ์ด์œ ๋Š”, ์ด ๋‘ ํŒŒ์ผ์ค‘ ํ•˜๋‚˜๊ฐ€ ํฌ๋ ˆ์ดํŠธ์˜ ๋ชจ๋“ˆ๊ตฌ์กฐ ๋ฃจํŠธ์ธ ๋ชจ๋“ˆ crate๋ฅผ ์„ ์–ธํ•˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

  • ๊ทธ๋ฆฌ๊ณ  ๊ทธ ์•„๋ž˜์— ๋งˆ์น˜ ํŒŒ์ผ์‹œ์Šคํ…œ์ฒ˜๋Ÿผ ๋ชจ๋“ˆ์ด ๊ตฌ์กฐํ™” ๋  ์ˆ˜ ์žˆ๋‹ค.

7.3 Paths for Referring to an Item in the Module Tree

  • ์œ„์— ์‚ด์ง ์–ธ๊ธ‰ํ•œ ๊ฒƒ์ฒ˜๋Ÿผ ์šฐ๋ฆฌ๊ฐ€ ๋Ÿฌ์ŠคํŠธ์—์„œ ๋ชจ๋“ˆ์˜ ์•„์ดํ…œ์„ ์ฐพ๊ธฐ ์œ„ํ•ด์„œ๋Š” ํŒŒ์ผ์‹œ์Šคํ…œ์ฒ˜๋Ÿผ path๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ์ฐพ์•„๋‚˜๊ฐ€๊ฒŒ ๋œ๋‹ค.

  • path๋Š” ๋‘๊ฐ€์ง€ ํ˜•ํƒœ๊ฐ€ ์žˆ๋‹ค.

    • absolute path : ํฌ๋ ˆ์ดํŠธ์˜ ๋ฃจํŠธ์—์„œ ์‹œ์ž‘ํ•˜๋Š” ๊ฒฝ๋กœ, ๋ฃจํŠธ๋Š” crate๋‚˜ mod ํ‚ค์›Œ๋“œ๋กœ ์„ ์–ธ๋œ ๋ชจ๋“ˆ์ด๋‹ค.
    • relative path : ํ˜„์žฌ ๋ชจ๋“ˆ์—์„œ ์‹œ์ž‘ํ•˜๋Š” ๊ฒฝ๋กœ, self, super, ํ˜น์€ ์•„์ดํ…œ์˜ ์ด๋ฆ„์œผ๋กœ ์‹œ์ž‘ํ•œ๋‹ค.
  • ๋‘ ๊ฒฝ๋กœ ๋ชจ๋‘, ํ•˜๋‚˜ ์ด์ƒ์˜ ์‹๋ณ„์ž์™€ :: ๋กœ ๊ตฌ๋ถ„๋œ๋‹ค.

  • ์•„๋ž˜๋Š” crate์˜ ๋ฃจํŠธ ๋ชจ๋“ˆ์—์„œ front_of_house๋ชจ๋“ˆ์˜ hosting๋ชจ๋“ˆ์˜ add_to_waitlistํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜๋Š” ์˜ˆ์ œ์ด๋‹ค.

mod front_of_house {
    mod hosting {
        fn add_to_waitlist() {}
    }
}

pub fn eat_at_restaurant() {
    // Absolute path
    crate::front_of_house::hosting::add_to_waitlist();

    // Relative path
    front_of_house::hosting::add_to_waitlist();
}
  • ์ฒซ ๋ฒˆ์งธ ํ˜ธ์ถœ์€ crate์˜ ๋ฃจํŠธ ๋ชจ๋“ˆ์—์„œ ์‹œ์ž‘ํ•˜๋Š” absolute path์ด๋‹ค.

  • ๋‘ ๋ฒˆ์งธ ํ˜ธ์ถœ์€ ํ˜„์žฌ ๋ชจ๋“ˆ์—์„œ ์‹œ์ž‘ํ•˜๋Š” relative path์ด๋‹ค.

๊ฐ€๋Šฅํ•˜๋ฉด ์ƒ๋Œ€๊ฒฝ๋กœ ๋Œ€์‹  ์ ˆ๋Œ€๊ฒฝ๋กœ๋ฅผ ์ง€์ •ํ•˜๋Š” ๊ฒƒ์ด ๋‚ซ๋‹ค๊ณ  ์ด์•ผ๊ธฐํ•œ๋‹ค. ๋‹น์—ฐํžˆ ์ผ์žฅ์ผ๋‹จ์€ ์žˆ์ง€๋งŒ, ์ฝ”๋“œ ์ˆ˜์ •์ด๋‚˜ ๋ฆฌํŒฉํ† ๋ง์„ ํ•  ๋•Œ, ์ˆ˜์ •์ด ์–ผ๋งˆ๋‚˜ ์ ์–ด์งˆ์ง€๊ฐ€ ๊ฐ€์žฅ ์ฃผ์š”ํ•œ ๊ธฐ์ค€์ด๋ผ๊ณ  ํ–ˆ์„ ๋•Œ, ์ฝ”๋“œ ์ •์˜์™€ ํ˜ธ์ถœ์„ ์„œ๋กœ ๋…๋ฆฝ์ ์œผ๋กœ ์ด๋™ํ•˜๋ ค๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋” ๋งŽ์ด ๋•Œ๋ฌธ์ด๋ผ๊ณ  ์ด์•ผ๊ธฐํ•œ๋‹ค. ํ˜ธ์ถœํ•˜๋Š” ๋ถ€๋ถ„์ด ์ •์˜๋œ ๋ถ€๋ถ„์— ๋Œ€ํ•œ ์ƒ๋Œ€๊ฒฝ๋กœ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด, ์ •์˜๋œ ๋ถ€๋ถ„์ด ๋‹ค๋ฅธ๊ณณ์œผ๋กœ ์˜ฎ๊ฒจ์กŒ์„ ๋•Œ, ํ˜ธ์ถœํ•˜๋Š” ๋ถ€๋ถ„์˜ ๋ณ€๊ฒฝ์ด ์ƒ๊ธฐ๋Š” ์ƒํ™ฉ์ด ๋” ์ž์ฃผ ๋ฐœ์ƒํ•œ๋‹ค.

  • ๋ฌดํŠผ ์œ„์˜ ์ฝ”๋“œ๋Š” ๋™์ž‘ํ•˜์ง€ ์•Š๋Š”๋‹ค. ์™œ๋ƒํ•˜๋ฉด add_to_waitlistํ•จ์ˆ˜๋Š” private์ด๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

  • ์ด๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” pubํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค.

mod front_of_house {
    pub mod hosting {
        pub fn add_to_waitlist() {}
    }
}

7.4.1 Starting Relative Paths with super

  • super ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด ์ƒ์œ„ ๋ชจ๋“ˆ๋กœ ์ด๋™ํ•  ์ˆ˜ ์žˆ๋‹ค.

  • file system์—์„œ ..๊ณผ ๊ฐ™์€ ์—ญํ• ์„ ํ•œ๋‹ค.

  • ๋ถ€๋ชจ ๋ชจ๋“ˆ๊ณผ ์ž์‹ ๋ชจ๋“ˆ์ด ์—ฐ๊ด€๋˜์–ด ์žˆ์„ ๋•Œ, rearrange๋ฅผ ํ•  ๋•Œ ์œ ์šฉํ•˜๋‹ค.

Filename : src/lib.rs

fn deliver_order() {}

mod back_of_house {
    fn fix_incorrect_order() {
        cook_order();
        super::deliver_order();
    }

    fn cook_order() {}
}
  • ์ด๋Ÿฌํ•œ ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•˜๋Š” ์ด์œ ๋Š”, ๋ชจ๋“ˆ์˜ ๊ตฌ์กฐ๋ฅผ ๋ณ€๊ฒฝํ•  ๋•Œ, ์ฝ”๋“œ๋ฅผ ์ˆ˜์ •ํ•˜์ง€ ์•Š๊ณ ๋„ ๋ชจ๋“ˆ์˜ ์œ„์น˜๋ฅผ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ด์ฃผ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

7.3.2 Making Structs and Enums Public

  • ๊ตฌ์กฐ์ฒด์™€ ์—ด๊ฑฐํ˜•์—์„œ๋„ pubํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด ์ ‘๊ทผ์„ฑ์„ ์ œ์–ด ํ•  ์ˆ˜ ์žˆ์ง€๋งŒ, ์ถ”๊ฐ€์ ์œผ๋กœ ์•Œ์•„์•ผ ํ•  ๊ฒƒ์ด ์žˆ๋‹ค.

  • ๊ตฌ์กฐ์ฒด๊ฐ€ public์ด๋ผ๊ณ  ํ•ด์„œ ๊ทธ ๊ตฌ์กฐ์ฒด์˜ ํ•„๋“œ๊ฐ€ public์ด ๋˜๋Š” ๊ฒƒ์€ ์•„๋‹ˆ๋‹ค.

  • ๊ตฌ์กฐ์ฒด์˜ ํ•„๋“œ๋ฅผ public์œผ๋กœ ๋งŒ๋“ค๋ ค๋ฉด, ๊ตฌ์กฐ์ฒด์˜ ํ•„๋“œ๋ฅผ 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"),
            }
        }
    }
}

pub fn eat_at_restaurant() {
    // 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");
}
  • ์œ„์˜ ์ฝ”๋“œ๋Š” private์œผ๋กœ ์„ ์–ธ๋œ seasonal_fruitํ•„๋“œ๊ฐ€ ์žˆ์–ด ํ•ด๋‹น ํ•„๋“œ๋ฅผ ์ ‘๊ทผํ•˜๋ ค๊ณ  ํ•˜๋ฉด ์ปดํŒŒ์ผ ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค.

  • ์‹ฌ์ง€์–ด ํ•ด๋‹น struct์˜ ์ธ์Šคํ„ด์Šค๋ฅผ ์ƒ์„ฑํ•  ๋•Œ๋„, ํ•ด๋‹น ํ•„๋“œ๋ฅผ ์ดˆ๊ธฐํ™” ํ•  ์ˆ˜ ์—†๊ธฐ ๋•Œ๋ฌธ์—, summer์™€ ๊ฐ™์€ ์ƒ์„ฑ์ž๋ฅผ ๋งŒ๋“ค์–ด์„œ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค.

  • ํŒจํ‚ค์ง€์™€ ํด๋ž˜์Šค์˜ ์˜์˜๊ฐ€ ์ƒ๋Œ€์ ์œผ๋กœ ๋ถˆ๋ถ„๋ช…ํ–ˆ๋˜ ์ž๋ฐ”์™€ ๋‹ฌ๋ฆฌ, ๋Ÿฌ์ŠคํŠธ๋Š” ํŒจํ‚ค์ง€์™€ ํฌ๋ ˆ์ดํŠธ์˜ ๊ฒฝ๊ณ„๊ฐ€ ๋ช…ํ™•ํ•˜๊ณ , ์–ด๋– ํ•œ ์˜๋ฏธ์—์„œ๋Š” ์บก์Šํ™”์— ๋Œ€ํ•œ ๋” ๋‹ค์–‘ํ•œ ์ œ์–ด๋ฅผ ์ œ๊ณตํ•œ๋‹ค.

  • ๋ฐ˜๋Œ€๋กœ enum์€ ๊ทธ ์ž์ฒด๋กœ public์ด๋ฉด, ๊ทธ ์•ˆ์— ์žˆ๋Š” ๋ชจ๋“  variant๋“ค๋„ public์ด๋‹ค.

mod back_of_house {
    pub enum Appetizer {
        Soup,
        Salad,
    }
}

pub fn eat_at_restaurant() {
    let order1 = back_of_house::Appetizer::Soup;
    let order2 = back_of_house::Appetizer::Salad;
}

7.4 Bringing Paths into Scope with the use Keyword

  • use ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด, ๋ชจ๋“ˆ์˜ ์•„์ดํ…œ์„ ํ˜„์žฌ ์Šค์ฝ”ํ”„๋กœ ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์žˆ๋‹ค.

  • use ํ‚ค์›Œ๋“œ๋Š” ::๋ฅผ ์‚ฌ์šฉํ•ด ์ ‘๊ทผํ•ด์•ผ ํ•˜๋Š” ์•„์ดํ…œ์„ ๊ฐ„๋‹จํ•˜๊ฒŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ด์ค€๋‹ค.

mod front_of_house {
    pub mod hosting {
        pub fn add_to_waitlist() {}
    }
}

use crate::front_of_house::hosting;

pub fn eat_at_restaurant() {
    hosting::add_to_waitlist();
}
  • use ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด, symbolic link๋ฅผ ๋งŒ๋“œ๋Š” ๊ฒƒ๊ณผ ๊ฐ™๋‹ค.

  • use ๋Š” use๋ฅผ ์„ ์–ธํ•œ ์Šค์ฝ”ํ”„์—์„œ๋งŒ ์œ ํšจํ•˜๋‹ค.

mod front_of_house {
    pub mod hosting {
        pub fn add_to_waitlist() {}
    }
}

pub fn eat_at_restaurant() {
    use crate::front_of_house::hosting;
    hosting::add_to_waitlist();
}

fn seat_at_table() {
    hosting::add_to_waitlist(); // ์ปดํŒŒ์ผ ์—๋Ÿฌ
}

7.4.2 Creating Idiomatic use Paths

  • use ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด์„œ ํ•จ์ˆ˜๋ฅผ ํŠน์ •ํ•ด์„œ ๊ฐ€์ ธ์˜ค๋Š”๊ฒƒ์ด ์•„๋‹ˆ๋ผ ๋ชจ๋“ˆ์„ ๊ฐ€์ ธ์˜ค๋Š” ๊ฒƒ์ด ์˜์•„ ํ•  ์ˆ˜ ์žˆ๋‹ค.
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();
}
  • ๋ฌผ๋ก  ๊ฒฐ๊ณผ๋Š” ๊ฐ™๋‹ค.

  • ์ด๋ ‡๊ฒŒ ๋ถ€๋ชจ ๋ชจ๋“ˆ๊นŒ์ง€ ๋ถˆ๋Ÿฌ์˜ค๋ฉด, ์‹ค์ œ๋กœ ์‚ฌ์šฉ ํ•  ๋•Œ๋Š” hosting::add_to_waitlist์™€ ๊ฐ™์ด ์‚ฌ์šฉํ•ด์•ผ ํ•˜๊ณ , ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด์„œ ์ด ํ•จ์ˆ˜๊ฐ€ ๋กœ์ปฌ์— ์ •์˜๋˜์–ด ์žˆ์ง€ ์•Š๋‹ค๋Š” ๊ฒƒ์„ ์•Œ๋ฆฌ๋ฉด์„œ ์ตœ์†Œํ•œ์˜ ๊ฒฝ๋กœ๋ฅผ ์‚ฌ์šฉ ํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ด์ค€๋‹ค.

  • ๋ฐ˜๋Œ€๋กœ use ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด ํ•จ์ˆ˜๋ฅผ ๊ฐ€์ ธ์˜ค๋ฉด ํ•ด๋‹น ํ•จ์ˆ˜๊ฐ€ ์–ด๋””์— ์ •์˜๋˜์–ด์žˆ๋Š”์ง€ ํ—ท๊ฐˆ๋ฆด ์ˆ˜ ์žˆ๋‹ค.

  • ๋ฐ˜๋Œ€๋กœ struct๋‚˜ enum์„ ๊ฐ€์ ธ์˜ฌ ๋•Œ๋Š”, ์ „์ฒด ๊ฒฝ๋กœ๋ฅผ ๋ช…์‹œํ•˜๋Š”๊ฒƒ์ด ๊ด€์šฉ์ ์ด๋‹ค.

use std::collections::HashMap;

fn main() {
    let mut map = HashMap::new();
    map.insert(1, 2);
}
  • ๋”ฑํžˆ ์ด์œ ๋Š” ์—†๊ณ , ๊ทธ๋ƒฅ ๊ด€์šฉ์ ์ธ ๋ฐฉ๋ฒ•์ด๋‹ค.

  • ๋˜ํ•œ use ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด ์—ฌ๋Ÿฌ๊ฐœ์˜ ์•„์ดํ…œ์„ ๊ฐ€์ ธ์˜ฌ ๋•Œ ๊ฐ™์€ ์ด๋ฆ„์„ ๊ฐ€์ง„ ์•„์ดํ…œ์„ ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์—†๋‹ค.

use std::fmt;
use std::io;

fn function1() -> fmt::Result {
    // --snip--
}

fn function2() -> io::Result<()> {
    // --snip--
}

๋งŒ์•ฝ ์•„๋ž˜์™€ ๊ฐ™์ด ์‚ฌ์šฉํ•˜๋ฉด ์ปดํŒŒ์ผ ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค.

use std::fmt::Result;
use std::io::Result;

fn function1() -> Result {
    // --snip--
}

fn function2() -> Result<()> {
    // --snip--
}
  • ์ด๋Ÿฐ ๊ฒฝ์šฐ์—๋Š” as ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด ๋‹ค๋ฅธ ์ด๋ฆ„์œผ๋กœ ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์žˆ๋‹ค.
use std::fmt::Result;
use std::io::Result as IoResult;

fn function1() -> Result {
    // --snip--
}

fn function2() -> IoResult<()> {
    // --snip--
}

7.4.3 Re-exporting Names with pub use

  • ๊ธฐ๋ณธ์ ์œผ๋กœ use ํ‚ค์›Œ๋“œ๋กœ ๊ฐ€์ ธ์˜จ ์•„์ดํ…œ์€ private์ด๋‹ค.

  • ๋งŒ์•ฝ ๊ฐ€์ ธ์˜จ ์•„์ดํ…œ์„ public์œผ๋กœ ๋งŒ๋“ค๊ณ  ์‹ถ๋‹ค๋ฉด, pub use ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋œ๋‹ค.

mod front_of_house {
    pub mod hosting {
        pub fn add_to_waitlist() {}
    }
}

pub use crate::front_of_house::hosting;

pub fn eat_at_restaurant() {
    hosting::add_to_waitlist();
}
  • ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด hosting๋ชจ๋“ˆ์€ public์ด ๋˜๊ณ , add_to_waitlistํ•จ์ˆ˜๋„ public์ด ๋œ๋‹ค.

  • restaurant::front_of_house::hosting::add_to_waitlist์™€ ๊ฐ™์ด ์‚ฌ์šฉํ•ด์•ผ ํ•˜๋Š” ๊ฒƒ์„.

  • restaurant::hosting::add_to_waitlist์™€ ๊ฐ™์ด ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

  • ์ด๋Ÿฌํ•œ ๊ธฐ๋Šฅ์€, ์ฝ”๋“œ์˜ ๋‚ด๋ถ€ ๊ตฌ์กฐ๊ฐ€ ์ด ์ฝ”๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋ ค๋Š” ํ”„๋กœ๊ทธ๋ž˜๋จธ์—๊ฒŒ ๋…ธ์ถœ๋˜์ง€ ์•Š๋„๋ก ํ•  ์ˆ˜ ์žˆ๋‹ค. (๋…ธ์ถœ๋˜์ง€ ์•Š์œผ๋ฏ€๋กœ ๋ณ€๊ฒฝ๋˜์–ด๋„ ์˜ํ–ฅ์„ ๋ฐ›์ง€ ์•Š๊ณ , ๋„๋ฉ”์ธ์— ๋Œ€ํ•œ ์ƒ๊ฐ์„ ํ•˜์ง€ ์•Š์„ ์ˆ˜ ์žˆ๋‹ค.)

7.4.4 Using External Packages

  • ์™ธ๋ถ€ ํŒจํ‚ค์ง€๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ๋Š”, Cargo.toml์— ์˜์กด์„ฑ์„ ์ถ”๊ฐ€ํ•˜๊ณ , use ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์žˆ๋‹ค.

  • use ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด ๊ฐ€์ ธ์˜ฌ ๋•Œ๋Š”, ํŒจํ‚ค์ง€์˜ ์ด๋ฆ„์„ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค.

[dependencies]
rand = "0.8.3"
use rand::Rng;

fn main() {
    let secret_number = rand::thread_rng().gen_range(1..101);
    println!("The secret number is: {}", secret_number);
}
  • ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด randํŒจํ‚ค์ง€์˜ Rng trait์„ ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์žˆ๋‹ค.

  • Rng trait์˜ ์Šค์ฝ”ํ”„์— ์žˆ๋Š” thread_rngํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜๊ณ , gen_rangeํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœ ํ•  ์ˆ˜ ์žˆ๋‹ค.

7.5 Separating Modules into Different Files

  • ํŒŒ์ผ์‹œ์Šคํ…œ๊ณผ ์ด๋ฆ„์„ ์กฐ๊ธˆ ์ƒ๊ฐํ•ด๋‘๋ฉด ๋”ฑํžˆ ์–ด๋ ค์šด๊ฑด ์—†๊ธฐ์— ์ƒ๋žต~