π¦ Rust Master Class - Chapter 8: Rust Modules
My closet was a disaster. Clothes, books, cables β all tangled. I organized it into drawers. That's exactly what Rust modules are: not file organization, but a philosophy of clarity.
Rustβs module system is a powerful set of tools used to manage code organization, scope, and privacy. It is composed of packages, crates, and modules [1-3].
1. Packages and Crates
- Crate: The smallest amount of code the Rust compiler considers at one time. Crates can be binary (producing an executable) or library (intended for reuse by other programs) [1-3].
- Package: One or more crates that provide a specific set of functionality. A package contains a
Cargo.tomlfile which describes how to build those crates [1-3].
2. Modules for Organization and Privacy
Modules allow you to group related code (functions, structs, traits) together within a crate for better readability and reuse [1-3].
- Privacy Rules: Items within a module are private by default. To make an item (like a function or struct) accessible to a parent module or external code, you must explicitly mark it with the
pubkeyword [4-6]. - Encapsulation: Struct fields also remain private unless marked
pub. For example, in anOrderstruct, you might keep thequantityfield private while making the struct itself public .
3. Organizing Code Across Files
Rust allows you to separate modules into different files to prevent a single file from becoming too large [1-3].
- In-file Definition: You can define a module directly in a file using
mod name { ... }. - Declaration in Separate Files: If you declare a module using
mod file_name;(without a block), Rust looks for a file namedfile_name.rsor a directory namedfile_name/mod.rsto find the module's code [4, 9-11]. - Hierarchy: The crate root (typically
src/main.rsfor binary crates orsrc/lib.rsfor libraries) acts as the starting point of the module tree [1-3, 9].
4. Paths and the use Keyword
To refer to an item in the module tree, you use a path [1-3].
- Absolute Path: Starts from the crate root using the keyword
crate. - Relative Path: Starts from the current module and uses
self,super, or an identifier in the current module [1-5]. -
useKeyword: You can bring a path into scope once with theusekeyword so you can call its items more concisely thereafter [1-5].
5. Advanced Organization: Re-exporting and External Crates
- Re-exporting (
pub use): This technique allows you to take an item from one module and make it available in another. It is often used to provide a simplified public API for a library, allowing users to import items from the root instead of deep internal paths . - External Crates: You can incorporate functionality from the community by adding external packages as dependencies in your
Cargo.tomlfile .
Code Example: Basic Module Structure
// In src/lib.rs
pub mod product {
pub struct Item {
pub id: u424,
price: f424, // Private field
}
}
// In src/main.rs
use crate::product::Item; // Bringing into scope
fn main() {
// Create a new variable
let my_item = Item { id: 1, price: 10.0 }; // Error: price is private
}
[Sources: 1, 5, 18, 246, 247, 248, 249]
π Download the full PDF: https://drive.google.com/file/d/1WvdgtrhCbJEf5sQF5o9GFPTZ2SReW2Da/view?usp=sharing
Part 8 of the Rust Master Class series β STEM EdTech | Automation Consulting | Rust Tutoring
RustLang #Programming #LearnToCode #STEM #EdTech
π Practice Resources
GitHub Repository: https://github.com/PacktPublishing/Rust-Programming-Master-Class-from-Beginner-to-Expert
Try it yourself: https://play.rust-lang.org/
Run the code from this chapter in the Rust playground, then clone the repo to continue your Rust journey!
Part 8 of the Rust Master Class series β STEM EdTech | Automation Consulting | Rust Tutoring
Top comments (0)