소개
이 실습에서 코드 예제의 모듈 파일 계층 구조는 다음과 같이 나타낼 수 있습니다. "my"라는 디렉토리가 있으며, 이 디렉토리에는 "inaccessible.rs"와 "nested.rs"라는 두 개의 파일이 있습니다. 또한 "my.rs"라는 파일과 "split.rs"라는 파일이 있습니다. "split.rs" 파일에는 "my.rs" 파일에 정의된 "my" 모듈이 포함되어 있으며, "my.rs" 파일에는 각각 "inaccessible.rs"와 "nested.rs" 파일에 정의된 "inaccessible"와 "nested" 모듈이 포함되어 있습니다.
참고: 실습에서 파일 이름을 지정하지 않으면 원하는 파일 이름을 사용할 수 있습니다. 예를 들어,
main.rs를 사용하고rustc main.rs && ./main으로 컴파일 및 실행할 수 있습니다.
파일 계층 구조
모듈은 파일/디렉토리 계층 구조에 매핑될 수 있습니다. 파일에서 가시성 예제를 살펴보겠습니다.
$ tree .
.
├── my
│ ├── inaccessible.rs
│ └── nested.rs
├── my.rs
└── split.rs
split.rs에서:
// 이 선언은 `my.rs` 라는 파일을 찾고
// 이 범위 내의 `my` 라는 모듈 안에 그 내용을 삽입합니다.
mod my;
fn function() {
println!("called `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!("called `my::function()`");
}
fn private_function() {
println!("called `my::private_function()`");
}
pub fn indirect_access() {
print!("called `my::indirect_access()`, that\n> ");
private_function();
}
my/nested.rs에서:
pub fn function() {
println!("called `my::nested::function()`");
}
#[allow(dead_code)]
fn private_function() {
println!("called `my::nested::private_function()`");
}
my/inaccessible.rs에서:
#[allow(dead_code)]
pub fn public_function() {
println!("called `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()`
요약
축하합니다! 파일 계층 실습을 완료했습니다. LabEx 에서 더 많은 실습을 통해 기술을 향상시킬 수 있습니다.