Code Organization
Rust's "module system" helps structure your projects effectively. It gives you fine-grained control over code organization, determining which elements are publicly accessible and which remain private, as well as managing the names available in different parts of your program. This system is built upon:
- Modules enable you to organize your code hierarchically, define the scope in which names are valid, and control the privacy of items (via the
pub
keyword). - Paths provide a way to uniquely identify items within your code, such as structs, functions, or other modules. The
use
keyword creates shortcuts to paths. - Crates represent a logical unit of code, forming a tree of modules that compiles into either a library or an executable.
- Packages, managed by
Cargo
, allow you to build, test, and share your Rust crates. - Workspaces, used for large projects, are a set of packages that share the same dependencies.
Modules and Paths
Structure your Code into Modules |
Split your Code into Several Files |
Access Items Within Modules via Paths |
Use Modules to Hide Implementation Details |
Visibility
use
Declarations
Dependencies
Code Organization by Project Type and Size
Related Topics
- Package Layout.