[T.rust] Rust study - day5

Jaehwan Chung·2023년 2월 6일
0

T.rust

목록 보기
3/3

개요

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로 이름 가져오기

  • package : 패키지 생성및 공유할 수 있는 cargo기능
  • create : 가장 기본적인 소스파일
  • Modules : 조각조각들 모듈,
  • use :
  • Paths : 파일경로

Packages and Crates

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

Defining Modules to Control Scope and Privacy

모듈 가져오기

Filename: src/lib.rs

mod client;
mod network;

pub키워드

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();
}

use

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();와 같이 사용할 수 있게 된다.

super

mod tests {
    use super::client;

    fn it_works() {
        client::connect();
    }
}
use std::id{self,write};

과제

계산기 구조체 만들기

0개의 댓글