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
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.
main
that defines what happens when the executable runsmain
function, and don’t compile to an executable. They define functionality intended to be shared with multiple projects. Most of time, “crate” is interchangeable to “library“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.
We’ll start with a list of rules for references
mod <name>;
in the crate root file. The compiler will look for the module’s code in these places:
mod <name>
src/<name>.rs
src/<name>/mod.rs
src/garden.rs
file, you can declare mod vegetables;
. The compiler will look for the submodule’s code within the directory named for the parent.