As project grows, code should be organized by splitting it into multiple modules and then multiple files. A package can contain multiple binary crates and optionally one library crate.

Rust has a number of features to allow user to manage code’s organization. Those features are collectively referred as the module system, include

Packages and Crates

A crate is the smallest amount of code that the Rust compiler considers at a time. Crates can contain modules, and the modules may be defined in other files that get compiled with the crate.

A crate can either be a binary crate or a library crate.

A package is a bundle of one or more crates that provides a set of functionality. A package contains a Cargo.toml file that contains how to build those crates.

A package can contain as many binary crates as wished, but at most one library crate. A package also need to contain at least one crate, binary or library.

If a package contains src/main.rs and src/lib.rs, it has two crates: a binary and a library, both with the same name as the package. A package can have multiple binary crates by placing files in the src/bin directory: each file will be a separate binary crate.

Defining Modules to Control Scope and Privacy

We’ll start with a list of rules for references

Modules Cheat Sheet