파일 계층 구조
모듈은 파일/디렉토리 계층 구조에 매핑될 수 있습니다. 가시성 예제를 파일별로 나누어 봅시다:
$ tree .
.
├── my
│ ├── inaccessible.rs
│ └── nested.rs
├── my.rs
└── split.rs
split.rs에서:
// 이 선언은 `my.rs`라는 파일을 찾아 그 내용을
// 이 스코프 아래의 `my`라는 이름의 모듈로 삽입할 것입니다
mod my;
fn function() {
println!("`function()` 호출됨");
}
fn main() {
my::function();
function();
my::indirect_access();
my::nested::function();
}
my.rs에서:
// 마찬가지로 `mod inaccessible`과 `mod nested`는 `nested.rs`와
// `inaccessible.rs` 파일을 찾아 각각의 모듈 아래에
// 삽입할 것입니다
mod inaccessible;
pub mod nested;
pub fn function() {
println!("`my::function()` 호출됨");
}
fn private_function() {
println!("`my::private_function()` 호출됨");
}
pub fn indirect_access() {
print!("`my::indirect_access()` 호출됨, 결과는\n> ");
private_function();
}
my/nested.rs에서:
pub fn function() {
println!("`my::nested::function()` 호출됨");
}
#[allow(dead_code)]
fn private_function() {
println!("`my::nested::private_function()` 호출됨");
}
my/inaccessible.rs에서:
#[allow(dead_code)]
pub fn public_function() {
println!("`my::inaccessible::public_function()` 호출됨");
}
모든 것이 이전과 같이 여전히 잘 작동하는지 확인해 봅시다:
$ rustc split.rs && ./split
called `my::function()`
called `function()`
called `my::indirect_access()`, that
> called `my::private_function()`
called `my::nested::function()`