『TRPL Ch 7. Managing Growing Projects with Packages, Crates, and Modules』
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
를 사용하는 건 심볼릭 링크를 만드는 것과 비슷하다. 위의 경우 hosting
이 해당 스코프에서 유효한 이름이 된다. hosting
모듈은 crate root에 정의되어 있다.
- 표준 라이브러리에 있는 모듈을 가져올 수 있다.
use std::io
as
키워드로 이름을 부여할 수도 있다. use std::io::Result as IoResult
- 스코프로 모듈을 가져오면 private으로 가져오게 된다. 이것을 public으로 re-exporting할 수 있다.
pub use crate::front_of_house::hosting
- 외부 모듈을 설치해서 가져올 수 있다.
use
경로를 중첩할 수 있다. use std::{cmp::Ordering, io}
io
자기 자신과 io::Write
를 스코프로 가져올 때 중첩을 이용한다. use std::io::{self, Write}
- 모든 public 요소를 가져올 때는 glob 연산자
*
을 사용한다. use std::collections::*
Separating modules
- 모듈을 꼭 한 파일에 둘 필요는 없다.
src/lib.rs
파일이 있다.
mod front_of_house;
pub use crate::front_of_house::hosting;
pub fn eat_at_restaurant() {
hosting::add_to_waitlist();
}
- 그리고
src/front_of_house.rs
파일이 있다.
pub mod hosting {
pub fn add_to_waitlist() {}
}
이 문서를 인용한 문서