liveBook · Manning

Module

As explained in Code Like a Pro in Rust

A module is a distinct, bounded software unit that groups related code or functionality and provides an organizational or interaction boundary within a larger system.

A module groups related Rust code into a hierarchical unit and provides a namespace for its symbols. Modules can be nested or split across source files. section 2.9

Use modules to organize code and control visibility:

  • Declare an inline module with `mod name { ... }`, or use `mod name;` to include a module from another source file. section 2.9
  • Declarations are private by default. Use `pub` to expose items, and `pub(crate)` to make an item available within the crate but not outside it. section 2.9
  • A public item inside a private module is not thereby exported outside the crate; the containing module's visibility also matters. section 2.9
mod private_mod {
  // ...              #1
}
pub mod public_mod {
  // ...              #2
}

The declarations contrast private and public modules; use module visibility to shape which names are exposed. section 2.9

Module hierarchies can be nested deeply. Rust conventions use snake case for module names, helping distinguish them from most other names in `use` statements. section 2.9

How Code Like a Pro in Rust covers it

How deep it goes
Explains `mod` declarations, nested and file-backed modules, default privacy, and visibility modifiers such as `pub(crate)`.
Examples
Demonstrates nested module declarations, separate source files, public and private modules, and a `use` import.
What you can do after
Apply Rust `mod` and `pub` declarations to organize code and control symbol visibility.
How it connects
Connects modules with crates, namespaces, visibility, and `use` imports.

Also explained in

Compare how 35 books explain module →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Code Like a Pro in Rust. The section links point to the parts of the book it draws on.