수색…
통사론
- mod modname ; // 동일한 디렉터리에있는 modname .rs 또는 modname /mod.rs에서 모듈을 검색합니다.
- mod modname { 블록 }
모듈 트리
파일 :
- example.rs (root of our modules tree, generally named lib.rs or main.rs when using Cargo)
- first.rs
- second/
- mod.rs
- sub.rs
모듈 :
- example -> example
- first -> example::first
- second -> example::second
- sub -> example::second::sub
- third -> example::third
example.rs
pub mod first;
pub mod second;
pub mod third {
...
}
이 모듈은 second 선언해야 example.rs 상위는 파일로서 example , 그리고, 예를 들면 first 따라서 선언 할 수 first.rs 또는 같은 디렉토리 레벨에서 다른 파일
second/mod.rs
pub mod sub;
# [경로] 속성
Rust의 #[path] 속성은 표준 위치에 있지 않은 특정 모듈을 검색 할 경로를 지정하는 데 사용할 수 있습니다. 그러나 일반적으로 모듈 계층 구조가 취약 해지고 완전히 다른 디렉토리에서 파일을 이동하여 쉽게 빌드를 중단 할 수 있기 때문에 일반적으로 사용하지 않는 것이 좋습니다 .
#[path="../path/to/module.rs"]
mod module;
코드의 이름과 '사용'의 이름
use 문에있는 이름의 이중 콜론 구문은 코드의 다른 곳에서 사용 된 이름과 유사하지만 이러한 경로의 의미는 다릅니다.
기본적으로 use 문에있는 이름은 crate root에서 시작하여 절대적인 것으로 해석됩니다. 코드의 다른 곳의 이름은 현재 모듈과 관련이 있습니다.
진술 :
use std::fs::File;
모듈뿐만 아니라 크레이트의 주 파일에서도 동일한 의미를가집니다. 반면에 std::fs::File::open() 과 같은 함수 이름은 코드의 이름이 현재 모듈을 기준으로 해석되기 때문에 상자의 기본 파일에서만 Rust의 표준 라이브러리를 참조합니다.
fn main() {
std::fs::File::open("example"); // OK
}
mod my_module {
fn my_fn() {
// Error! It means my_module::std::fs::File::open()
std::fs::File::open("example");
// OK. `::` prefix makes it absolute
::std::fs::File::open("example");
// OK. `super::` reaches out to the parent module, where `std` is present
super::std::fs::File::open("example");
}
}
std::… names를 추가 할 수있는 상자의 루트와 같은 모든 곳에서 행동하게하려면 :
use std;
반대로 self 키워드 나 super 키워드를 접두어로 사용하여 상대 경로를 use 수 있습니다.
use self::my_module::my_fn;
상위 모듈 액세스
때로는 프로젝트에서 절대 경로와 함께 무언가를 use 하지 않고도 함수와 구조체를 상대적으로 가져 오는 것이 유용 할 수 있습니다. 이를 위해 다음과 같이 super 모듈을 사용할 수 있습니다.
fn x() -> u8 {
5
}
mod example {
use super::x;
fn foo() {
println!("{}", x());
}
}
super 여러 번 사용하여 현재 모듈의 '조부모'에게 접근 할 수 있지만 한 번 가져 오기에서 super 너무 많이 사용하면 가독성 문제가 발생하는 것을 조심해야합니다.
수출 및 가시성
디렉토리 구조 :
yourproject/
Cargo.lock
Cargo.toml
src/
main.rs
writer.rs
main.rs
// This is import from writer.rs
mod writer;
fn main() {
// Call of imported write() function.
writer::write()
// BAD
writer::open_file()
}
작가 .rs
// This function WILL be exported.
pub fn write() {}
// This will NOT be exported.
fn open_file() {}
기본 코드 구성
코드베이스가 커지면 어떻게 코드를 구성 할 수 있는지 보도록하겠습니다.
01. 기능들
fn main() {
greet();
}
fn greet() {
println!("Hello, world!");
}
02. 모듈 - 동일한 파일에 있음
fn main() {
greet::hello();
}
mod greet {
// By default, everything inside a module is private
pub fn hello() { // So function has to be public to access from outside
println!("Hello, world!");
}
}
03. 모듈 - 같은 디렉토리에있는 다른 파일에 있음
일부 코드를 새 파일로 옮길 때 mod 선언에 코드를 래핑 할 필요가 없습니다. 파일 자체는 모듈로 작동합니다.
// ↳ main.rs
mod greet; // import greet module
fn main() {
greet::hello();
}
// ↳ greet.rs
pub fn hello() { // function has to be public to access from outside
println!("Hello, world!");
}
일부 코드를 새 파일로 옮길 때 해당 코드가
mod선언에서 래핑 된 경우 해당 파일의 하위 모듈이됩니다.
// ↳ main.rs
mod greet;
fn main() {
greet::hello::greet();
}
// ↳ greet.rs
pub mod hello { // module has to be public to access from outside
pub fn greet() { // function has to be public to access from outside
println!("Hello, world!");
}
}
04. 모듈 - 다른 디렉토리에있는 다른 파일에 있음
일부 코드를 다른 디렉토리의 새 파일로 이동하면 디렉토리 자체가 모듈로 작동합니다. 모듈 루트의 mod.rs 는 디렉토리 모듈의 시작점입니다. 해당 디렉토리의 다른 모든 파일은 해당 디렉토리의 하위 모듈로 작동합니다.
// ↳ main.rs
mod greet;
fn main() {
greet::hello();
}
// ↳ greet/mod.rs
pub fn hello() {
println!("Hello, world!");
}
모듈 루트에 여러 개의 파일이있을 때,
// ↳ main.rs
mod greet;
fn main() {
greet::hello_greet()
}
// ↳ greet/mod.rs
mod hello;
pub fn hello_greet() {
hello::greet()
}
// ↳ greet/hello.rs
pub fn greet() {
println!("Hello, world!");
}
05. 모듈 - self 와 함께
fn main() {
greet::call_hello();
}
mod greet {
pub fn call_hello() {
self::hello();
}
fn hello() {
println!("Hello, world!");
}
}
06. 모듈 - super
- 모듈 내부에서 루트 함수에 액세스하려면,
fn main() {
dash::call_hello();
}
fn hello() {
println!("Hello, world!");
}
mod dash {
pub fn call_hello() {
super::hello();
}
}
- 중첩 모듈 내부에서 외부 / 부모 모듈의 함수에 액세스하려면,
fn main() {
outer::inner::call_hello();
}
mod outer {
pub fn hello() {
println!("Hello, world!");
}
mod inner {
pub fn call_hello() {
super::hello();
}
}
}
07. 모듈 - use
- 전체 경로를 새 이름으로 바인딩하려는 경우,
use greet::hello::greet as greet_hello;
fn main() {
greet_hello();
}
mod greet {
pub mod hello {
pub fn greet() {
println!("Hello, world!");
}
}
}
- 크레이트 범위 수준 콘텐츠를 사용하려는 경우
fn main() {
user::hello();
}
mod greet {
pub mod hello {
pub fn greet() {
println!("Hello, world!");
}
}
}
mod user {
use greet::hello::greet as call_hello;
pub fn hello() {
call_hello();
}
}