Modules & Crates
Rust is a systems programming language focused on speed, memory safety, and fearless concurrency — and it organises growing codebases with modules for namespacing and crates for distribution.
Learn Modules & Crates in our free Rust course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a quick reference.
Part of the free Rust course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.
In this lesson you'll declare modules with mod , control visibility with pub , navigate with paths, shorten names with use , and pull in external crates.
What You'll Learn in This Lesson
1️⃣ Modules, Visibility, and Paths
A mod block creates a namespace. Items inside are private by default ; add pub to expose them. You reach an item by its path , joining names with :: — for example math::advanced::square .
Notice secret is private, so main can't call it — but reveal , which lives in the same module, can. That's encapsulation: the module hides its internals.
2️⃣ The use Keyword and Crates
Writing long paths repeatedly is tedious, so use brings a path into scope under a short name. The same mechanism imports the standard library (the std crate) and external crates you add in Cargo.toml — Cargo downloads and builds them for you.
After use shapes::Circle; we wrote just Circle . To pull in a crate like rand , you'd add it under [dependencies] in Cargo.toml and then use it the same way.
Your turn. Fill in the blanks marked ___ , then run it.
Build a module with two public conversion functions and call them via use . Run it with cargo run .
📋 Quick Reference — Modules & Crates
Practice quiz
Which keyword declares a module?
- module
- namespace
- mod
- pkg
Answer: mod. mod creates a named module that groups related code.
By default, items inside a module are:
- Private
- Public
- Mutable
- Static
Answer: Private. Items are private by default; you expose them with pub.
Which keyword makes an item accessible outside its module?
- open
- export
- extern
- pub
Answer: pub. pub marks an item as public so callers outside the module can use it.
What separates names in a Rust path?
- A dot .
- Two colons ::
- A slash /
- An arrow ->
Answer: Two colons ::. Paths join names with ::, like math::advanced::square.
What does the use keyword do?
- Brings a path into scope under a short name
- Copies code into the file
- Declares a module
- Runs a function
Answer: Brings a path into scope under a short name. use creates a local shortcut to a name that exists elsewhere.
What is a crate?
- A single function
- A type of variable
- The unit of compilation and distribution
- A trait
Answer: The unit of compilation and distribution. A crate is a whole library or binary; modules organize code inside it.
Can a private function be called from elsewhere in the same module?
- No, never
- Yes
- Only with unsafe
- Only if it is static
Answer: Yes. Code in the same module can call its own private items, like reveal calling secret.
Does a pub struct automatically make all its fields public?
- Yes
- Only numeric fields
- Only the first field
- No, each field needs pub too
Answer: No, each field needs pub too. A pub struct still has private fields unless each field is also marked pub.
How do you import several names in one use statement?
- use a, b;
- use std::cmp::{min, max};
- use std::cmp::min+max;
- You cannot
Answer: use std::cmp::{min, max};. Braces group imports, like use std::cmp::{min, max};.
Where do you add an external crate from crates.io?
- In main.rs
- With a mod statement
Add it under [dependencies] in Cargo.toml, then use it in code.