citrus327 / issue-blog-record

2 stars 2 forks source link

Rust学习笔记 - Module System #23

Closed citrus327 closed 4 years ago

citrus327 commented 4 years ago

Module System

Rust has a number of features that allow you to manage your code’s organization, including which details are exposed, which details are private, and what names are in each scope in your programs. These features, sometimes collectively referred to as the module system, include:

  • 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

ref

A package must contain zero or one library crates, and no more.

$ cargo new my-project
     Created binary (application) `my-project` package
$ ls my-project
Cargo.toml
src
$ ls my-project/src
main.rs

When we entered the command, Cargo created a Cargo.toml file, giving us a package. Looking at the contents of Cargo.toml, there’s no mention of src/main.rsbecause Cargo follows a convention that src/main.rs is the crate root of a binary crate with the same name as the package. Likewise, Cargo knows that if the package directory contains src/lib.rs, the package contains a library crate with the same name as the package, and src/lib.rs is its crate root. Cargo passes the crate root files to rustc to build the library or binary.

Here, we have a package that only contains src/main.rs, meaning it only contains a binary crate named my-project. If a package contains src/main.rs and src/lib.rs, it has two crates: a library and a binary, both with the same name as the package. A package can have multiple binary crates by placing files in the src/bin directory: each file will be a separate binary crate.

Modules and Paths

the use keyword that brings a path into scope; and the pub keyword to make items public.

Modules let us organize code within a crate into groups for readability and easy reuse. Modules also control the privacy of items, which is whether an item can be used by outside code (public) or is an internal implementation detail and not available for outside use (private).

mod front_of_house {
    pub mod hosting {
        pub fn add_to_waitlist() {}
    }
}

pub fn eat_at_restaurant() {
    // Absolute path
    crate::front_of_house::hosting::add_to_waitlist();

    // Relative path
    front_of_house::hosting::add_to_waitlist();
}

分离文件

|- src
  |- front_of_house // 模块文件夹
    |-hosting.rs    // 模块主体
  |- test_a_mod     // 模块文件夹
    |-test_mod.rs   // 模块主体
    front_of_house.rs // front_of_house的定义文件
    test_a_mod.rs     // test_a_mod的定义文件
    lib.rs            // 入口文件
// lib/src.rs
mod front_of_house;
mod test_a_mod;

pub use crate::front_of_house::hosting;
pub use crate::test_a_mod::test_mod;

pub fn eat_at_restaurant() {
    hosting::add_to_waitlist();
    hosting::add_to_waitlist();
    hosting::add_to_waitlist();

    test_mod::test_fn();
}
// src/front_of_house/hosting.rs
pub fn add_to_waitlist() {}
// src/test_a_mod/test_mod.rs
pub fn test_fn() {}
// src/front_of_house.rs
pub mod hosting;
// src/test_a_mod.rs
pub mod test_mod;