chap.07 Managing Growing Projects with Packages, Crates, and Modules
( 모듈을 사용하여 코드를 재사용하고 조직화하기 )
7.1 Packages and Crates
7.2 Defining Modules to Control Scope and Privacy
7.3 Paths for Referring to an Item in the Module Tree
7.4 Bringing Paths into Scope with the use Keyword
7.5 Separating Modules into Different Files
7.1 mod와 파일 시스템
7.2 pub으로 가시성 제어하기
7.3 use로 이름 가져오기
cargo new키워드 사용시 binary프로젝트가 만들어진다.
cargo new library --lib
lib
$ cargo new my-project
Created binary (application) `my-project` package
$ ls my-project
Cargo.toml
src
$ ls my-project/src
main.rs
Filename: src/lib.rs
mod client;
mod network;
pub으로 선언 될 경우 부모모듈 어디에서든 접근 가능.
private일 경우, 같은 파일내에있는 부모모듈 및 이 부모의 자식모듈에서만 접근 가능.
mod outermost {
pub fn middle_function() {}
fn middle_secret_function() {}
mod inside {
pub fn inner_function() {}
fn secret_function() {}
}
}
fn try_me() {
outermost::middle_function();
outermost::middle_secret_function();
outermost::inside::inner_function();
outermost::inside::secret_function();
}
pub mod a {
pub mod series {
pub mod of {
pub fn nested_modules() {}
}
}
}
use a::series::of;
fn main() {
of::nested_modules();
}
위와같이 use a::series::of;
선언하게 되면, main에서는 같은 레벨에 있게 되므로
of::nested_modules();
와 같이 사용할 수 있게 된다.
mod tests {
use super::client;
fn it_works() {
client::connect();
}
}
use std::id{self,write};
계산기 구조체 만들기