Modules & Cargo
While a program fits in one main.rs file, you can ignore how a project is organized. Real projects are split into parts, and Rust has three levels for that. A module is a named part of the code inside a crate: it is declared with the word mod right in a file or moved into a file of its own. A crate is what the compiler builds in one go, a program with main or a library. A package is a folder with a Cargo.toml that holds one or more crates.
Inside a crate a strict rule applies: everything is private until marked pub. This covers struct fields too, so pub struct does not yet open its data. A path such as crate::garden::water names an item in the module tree, and use gives it a short name.
From the outside, Cargo ties the project together. Cargo.toml lists the dependencies and the allowed versions, Cargo.lock records the exact versions Cargo picked, and features turn on optional parts of crates. A workspace unites several packages with a shared Cargo.lock and a shared target/ folder. Tools work around the build: cargo fmt evens out the style, cargo clippy hunts suspicious code, cargo doc builds the documentation. This topic walks through it all layer by layer, from the package to the lints.
Topic map
- Packages and Crates — a package describes the build in
Cargo.toml; a binary crate withmainand a library crate without it; thesrc/main.rsandsrc/lib.rsroots; programs insrc/bin/; a hyphen in the package name becomes an underscore. - Modules —
modwith a body in braces or in its own file;garden.rsorgarden/mod.rs; the module tree fromcrate; a file withoutmodis not compiled. - Visibility — everything is private by default;
pub,pub(crate),pub(super); struct fields are opened one by one (E0616); the variants of apub enumare public by themselves. - Paths and use — absolute paths from
crate::, relative ones fromself::andsuper::;use … as; re-exports withpub use;*imports and the prelude. - Cargo Dependencies —
[dependencies]andcargo add;"1.0"=^1.0=>=1.0.0, <2.0.0;Cargo.lockis kept in git;[dev-dependencies]; features are unified across the whole tree. - Workspaces —
[workspace]andmembers; a sharedCargo.lockandtarget/;[workspace.dependencies]andworkspace = true; commands with-p. - Tooling and Lints —
cargo fmt --checkandcargo clippy; theallow,expect,warn,deny,forbidlevels; the[lints]section;cargo doc; each crate has its own edition.
Common mistakes and traps
| Mistake | Consequence |
|---|---|
Creating src/utils.rs without mod utils; in the parent | The file is not part of the crate and is not compiled at all; the compiler "cannot find" its functions |
Writing mod utils; in every file that needs the module | mod declares a new module rather than importing the existing one: a repeat looks for another file (E0583) or compiles the module a second time |
Expecting pub struct to open the fields | Each field needs its own pub, otherwise E0616 on reading and E0451 on building a literal |
Putting pub on a function inside a private module | E0603 from outside: the whole path must be open, or the function is re-exported with pub use |
Expecting a use at the root to be visible in nested modules | A use works in one module; in a nested one the name is not found (E0425) |
Treating "1.0" in Cargo.toml as an exact version | It is the range >=1.0.0, <2.0.0; the exact versions are pinned by Cargo.lock |
| Making a feature that turns something off | Features are unified across the whole tree: another crate enables it and breaks a neighbour that needs the capability |
Why it matters in interviews
Modules and Cargo rarely come as a task of their own, but questions about them come up in almost every junior section: without them you cannot write a project bigger than one file. A candidate who has worked with real code is given away not by the syntax of mod but by knowing what is private by default, where a module's file lives and why Cargo.lock is needed.
What interviewers usually check:
- How a package differs from a crate and a module, and a binary crate from a library crate.
- How files map to modules:
mod,garden.rsorgarden/mod.rs, the module tree. - Visibility:
pub,pub(crate),pub(super)and the private fields of apub struct. - The
crate::,self::andsuper::paths,use … asand re-exports withpub use. - What
Cargo.tomlandCargo.lockhold, what"1.0"means and why the lock file is committed. - What a workspace is and why features must only add capabilities.