Code Organization

Rust's "module system" helps structure your projects effectively. It gives 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 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, are used 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

Visibility

use Declarations

Dependencies

Code Organization by Project Type

Naming Conventions

References

  • Package Layout.