본문 바로가기
Personal-Study/Rust

[Rust 문서 읽기] 7. Managing Growing Projects with Packages, Crates, and Modules (모듈)

by Aaron-Kim 2023. 2. 11.

The Rust Programming Language

- Module System

  - Packages: A Cargo feature that lets you build, test, and share crates

  - Crates: A tree of modules that produces a library or executable

  - Modules and use: Let you control the organization, scope, and privacy of paths

  - Paths: A way of naming an item, such as a struct, function, or module

- Packages and Crates

  - Crate: the smallest amount of code that the Rust compiler considers at a time

    - Crates can contain modules

    - binary crate or library crate

      - binary crate: must have a function called main

      - library crate: don't have a main function, don't compile to an executable ex) rand crate

  - Package: a bundle of one or more crates that provides a set of functionality

- Defining Modules to Control Scope and Privacy

  - use keyword that brings a path into scope

  - pub keyword make items public

  - as keyword

  - Modules Cheat Sheet

    - Start from the crate root

    - Declaring modules

    - Declaring submodules

    - Paths to code in modules

    - private vs. public

      - private is default

      - pub mod

    - The use keyword

  - Grouping Related Code in Modules

- Paths for Referring to an Item in the Module Tree

  - Path

    - absolute path (starts with crate)

    - relative path (self, super)

  - Exposing Paths with the pub keyword

    - making the module public doesn't make its contents public

    - The pub keyword on a module only lets code in its ancestor modules refer to it

  - Starting Relative Paths with super

  - Making Structs and Enums Public

    - enum public -> all of its variants are public

- Bringing Paths Into Scope with the use Keyword

  - similar to creating a symbolic link in the file system

  - Creating idomatic use Paths

    - Specifying the parent module when calling the function makes it clear

       that the function isn't locally defined while still minimizing repetition of the full path (ex. fn)

    - when bringing in structs, enums, and other items -> idiomatic way is to specify the full path

  - Providing New Names with the as keyword

    - alias (add on the end of Path)

  - Re-exporting Names with pub use

  - Using External Packages

  - Using Nested Paths to Clean Up Large use Lists

    - can use self in the nested path

  - The Glob Operator

    - * glob operator

    - often used when testing to bring everything under test into the tests module

- Separating Modules into Different Files


Managing Growing Projects with Packages, Crates, and Modules (영어)

모듈 (한국어)

반응형

댓글