Rust Package Crate Moudle

Module system

  • Package package: Build, test, share crate.
    • CRate unit package: A module tree that can generate a library or executable.
      • Module module, use: code organization, scope, private path.
        • Path: Way to name Struct, Function, Module and other projects.



  • 1 cargo.toml
  • 0 – 1 Library CRate
  • Arbitrary quantity binary crate
  • At least one CRAT (library / binary)


Crate Type: Binary / Library

crate root

Source code file, Rust compiler starts from here, constitutes CRATE Root MODULE.

  • Src / is a crate root of binary crate. The name of CRate is the same as the package name.
  • SRC / is the Crate root of library crate. The name of CRate is the same as the package name.

The file is placed inside the src / bin, which represents a separate binary crate on behalf of each file.

Crate combines the relevant functions into a scope to prevent conflicts.
eg: rand crate, you need to pass it by your name: Rand


In 1 CRATE, the code is packet. And can control privateity.

Establish Module:

  • MOD keyword
  • Can nested
mod CAR {
    mod honda {
        fn MPV(){}

    mod toyota {



Similar to Python Package Management.

Absolute path: Start with CREATE, use the CRATE name or a literal value CRate.
Relative path: Starting from the current module, using the identifier of the Self, Super, or the current module.

The path consists of at least one identifier, and the identifier is used ::

Mod car {mod honda {fn mpv () {}} mod toyota {}} fn driver () {// start Checking Crate :: Car :: Honda :: MPV () // from the root catalog CRATE ROOT Level Module Car :: Honda :: MPV ()}

If the call code and module code are always used, the relative path is used. Otherwise, use an absolute path.

Private border

The module default is private, if the function or the structure is put in, then it is private. All entries (functions, methods, structs, engines, modules, constants) in Rust are private.

Change into public method, keyword PUB

pub fn driver(){}

The parent module cannot access private entries in the submodule. Submodules can use all entries in all ancestors modules.

If both modules are same level, then they can be called each other without public.

Mod car {Pub MOD HONDA {Pub Fn MPV () {}} MOD TOYOTA {}} Pub Fn Driver ()}} Pub Fn Driver ()}} Pub Fn Driver ()}} Pub Fn Driver ()} You can call Car :: CRAT :: Car :: Honda :: MPV (); Car :: honda :: mpv ();


Access the content in the parent module path, similar to the file system ..

SUPER is also a relative path, accessing the superior directory.

pub struct

pub placed on the Struct to declare the structure, but the field inside is private, but also PUB declaration is public.

pub enum

Pub placed the enumeration in the public, the variant of the variants is public. (ENUM is to make others use)


Similar to Python’s import, or create a symbolic link.

Absolute path:use crate::CAR::honda::MPV;
Relative path:use CAR::honda::MPV

TheUse reference also needs to comply with private rules.

  • For functions, generally only introduce the parent module.
  • For Struct, Enum, others generally specify to the full path, itself, if two, the name is the same as the parent.
    use std::collections::HashMap;


Alias, like Python
use std::io::Result as ioResult;

pub use

Out out, similar to JS exposure.
Use USE to import the path (name) into the scope, this name is in this scope.​​Private

pub use crate::CAR::honda::MPV;
Introduced MPV and then exported.

Package external package

  1. Cargo.Toml Add a dependent package.
  2. ISE introduces a scope.

Standard library (STD) will also be treated as an external package.

Nested path

The same package or module introduces multiple entries.

The same part of the path: {differential part}
use std::{io, cmp::Ordering}

If the difference is just a relationship that is included, you can use Self.A reference is another referenced subpath.

USE std :: io; use std :: oose: o / 导 u u::: o μ {self, write};

* wildcard

use std::io::*

When testing, the module being tested is introduced into the TESTS module. Sometimes it is used to preview (prelude) modules.

Split file

When the module is defined, if the module name is behind; instead of the code block, RUST will load content from the file as the module.

mod CAR;
rust will find in the src directory.

The parent module is turned into a folder in a multi-level directory.