What are modules and crates in Rust and how does the module system work?
Learn how Rust modules and crates work: mod and pub visibility, paths, use, lib.rs vs main.rs, and Cargo, with code examples and interview questions answered.
Expected Interview Answer
A crate is the smallest compilation unit in Rust — either a binary or a library — while modules are named namespaces inside a crate that organize code and control visibility with the `mod` and `pub` keywords.
Every crate has a root module (`main.rs` for a binary or `lib.rs` for a library), and modules form a tree beneath it. Items are private by default and become accessible outside their module only when marked `pub`. You bring paths into scope with `use`, and multiple related crates are managed together in a Cargo workspace, with dependencies declared in `Cargo.toml`. This system gives Rust clear encapsulation and a predictable path-based way to reference code.
- Clear namespacing that avoids naming collisions
- Fine-grained visibility control via pub and private-by-default
- Logical grouping of related functions, types, and traits
- Reusable libraries distributed as crates on crates.io
- Cargo manages building, dependencies, and workspaces
AI Mentor Explanation
A crate is like a full cricket club, a self-contained organization you can field or share, while modules are its departments — batting, bowling, fielding coaching. Each department keeps its own drills private unless the club publishes them, and staff reference a drill by its department path. Cargo is the club administrator that assembles departments and brings in partner academies as dependencies.
Step-by-Step Explanation
Step 1
Pick a crate type
A binary crate has main.rs and produces an executable; a library crate has lib.rs and is meant to be reused.
Step 2
Declare modules
Use `mod name` to create a module inline or in a separate file/folder, forming a tree under the crate root.
Step 3
Set visibility
Items are private by default; mark functions, structs, or modules `pub` to expose them beyond their parent.
Step 4
Reference by path
Address items with paths like crate::utils::helper or self/super for relative navigation.
Step 5
Bring names into scope
Use `use` to shorten paths so you can call helper() instead of the full crate::utils::helper().
Step 6
Manage with Cargo
Declare dependencies in Cargo.toml and group multiple crates in a workspace for larger projects.
What Interviewer Expects
- Difference between a crate and a module
- Binary crate (main.rs) versus library crate (lib.rs)
- Items are private by default; pub exposes them
- How paths and the use keyword work
- Role of Cargo.toml and crates.io
- Awareness of workspaces for multi-crate projects
Common Mistakes
- Using crate and module interchangeably
- Forgetting items are private by default and omitting pub
- Thinking each file is automatically a public module
- Confusing crate::, self::, and super:: path prefixes
- Believing Cargo.toml defines modules rather than dependencies
Best Answer (HR Friendly)
“In Rust, a crate is a complete package of code that compiles into a program or a shared library, and modules are the folders inside it that group related code and decide what stays private or is shared. The Cargo tool ties it all together and pulls in outside libraries when needed.”
Code Example
// lib.rs — the crate root
mod math {
// private by default
fn secret() -> i32 { 42 }
// exposed to other modules
pub fn add(a: i32, b: i32) -> i32 {
a + b + secret()
}
}
use math::add;
pub fn demo() -> i32 {
add(1, 2) // calls math::add
}Follow-up Questions
- What is the difference between a binary crate and a library crate?
- How does the pub keyword control visibility across modules?
- What do crate::, self::, and super:: mean in a path?
- What is a Cargo workspace and when would you use one?
- How does Cargo resolve dependencies from crates.io?
- How do you split a module across multiple files?
MCQ Practice
1. What is a crate in Rust?
A crate is the unit the compiler builds at once, producing either an executable binary or a reusable library.
2. By default, items in a Rust module are?
Everything is private by default; you must add pub to make an item visible outside its module.
3. Which file is the root of a library crate?
lib.rs is the crate root for a library, while main.rs is the root for a binary crate.
Flash Cards
What is a crate? — The smallest compilation unit in Rust: a binary (main.rs) or a library (lib.rs).
What is a module? — A named namespace inside a crate, declared with mod, that groups code and controls visibility.
Default visibility in Rust? — Private — items are only visible in their module unless marked pub.
What does Cargo.toml do? — Declares the package metadata and its dependencies; Cargo uses it to build and fetch crates.
Continue Learning
Related Interview Questions
How does Cargo feature unification work, and how do you debug a feature that mysteriously turns on?
hard
Why are Rust build times slow, and what actually improves them?
medium
What Is the Difference Between a Python Module and a Package?
medium
What is Rust and what problems does it solve compared to C++?
easy