Module

A module is a unit of Rust code that encapsulates its content. Modules form a hierarchy (the module tree) within a crate. A module can correspond to a file or a part of a file. Submodules are usually grouped together in a directory, although that is not a necessity.

Inline modules
Modules can be declared with a block, these can be arbitrarily nested:

Modules are usually written in the global scope (outside of other items), but they can also be declared within a function:

A block creates a new global scope. This means that it can contain items, but no statements or expressions:

Visibility
Items can have a visibility modifier (e.g. ), which controls access from other modules. Example:

Possible visibility modifiers

 * No modifier: The item is only visible in the current module
 * : Equivalent to no modifier
 * : The item is only visible in the parent module
 * : The item is only visible in the current crate
 * : The item is only visible in the specified path. Any path can be specified, e.g., as long as it refers to an ancestor of the current module.
 * : The item is visible everywhere

Module paths
To access items in a different module, paths are used. A path is a sequence of path segments, separated by. Relative paths begin in the current module. Absolute paths, which start with the keyword, begin at the crate root. The parent module can be referred to with the keyword:

Root modules
The root module of a crate is the module that contains all other modules. If the crate is a binary, it also contains the  function (the entry point for the application).

In library crates, the root module resides in  by default. In binary crates, the default location is. This can be overridden. If a crate has multiple targets, it also has multiple module trees.

The module root of other crates is usually referenced with their crate name (substituting  with  ):

If there is both a binary target and a library target, the library can be referenced from the binary in the same way: