Sub topic 2
Modules and the module tree
This chapter assumes you can write basic Rust (functions, structs, Result), understand introductory ownership and borrowing, and can run cargo build and cargo run. Packages and crates were covered in the previous chapter. Here we look at how code inside one crate is grouped.
What a module is
A module is a named container for items such as functions, structs, enums, constants, traits, and other modules. Modules do two jobs. They group related code, and they set privacy boundaries. By default, everything inside a module is private to that module.
Every crate has an implicit root module. For a library this is src/lib.rs, and for a binary it is src/main.rs. Modules declared with mod become children of the module that declares them, so a crate forms a tree with the crate root at the top.
Declaring an inline module
The simplest form is mod name { ... } with the body written in place.
mod shapes {
pub struct Rect {
pub width: u32,
pub height: u32,
}
impl Rect {
pub fn area(&self) -> u32 {
self.width * self.height
}
}
fn private_helper() -> u32 {
42
}
pub fn answer() -> u32 {
private_helper()
}
}
fn main() {
let r = shapes::Rect { width: 3, height: 4 };
println!("{}", r.area());
println!("{}", shapes::answer());
// shapes::private_helper(); // error: function `private_helper` is private
}This program prints 12 and then 42. Three details matter here:
Rectispub, and so are its fields. Apub structwith private fields cannot be built with a struct literal from outside the module.private_helperhas nopub, so only code insideshapescan call it.answeris public and calls it, which shows that a module can expose a small interface over private details.- Code outside the module reaches items through a path, here
shapes::Rect. Paths are covered in the next chapter.
The module tree
For the example above, the tree looks like this:
The tree is a filesystem analogy that works well. The crate root is like /, modules are directories, and items are files. shapes and main are siblings, and both are children of the root.
Privacy rules in brief
The rule that surprises most newcomers is the direction of visibility:
- Code in a module can use private items defined in that module and in its ancestors.
- Code in a parent cannot see private items in a child. It can only see the child's
pubitems.
So main, in the root, can call shapes::answer() but not shapes::private_helper(). A child module, on the other hand, can freely use anything private in the root. The next chapters expand this into full visibility control.
Common mistakes
- Forgetting
pubon a function or field and getting an error such as "function is private". - Making a struct
pubbut leaving its fields private, then failing to construct it from outside. - Writing
mod shapes;(no body) without creating the matching file. That form tells the compiler to load the module from a file, which is covered in a later chapter.
Exercise
In the example, main calls shapes::answer() successfully, but shapes::private_helper() fails to compile. What makes the difference, and how would you allow the second call?
Show answer
answer is marked pub and private_helper is not, so only answer is visible outside shapes. Adding pub before fn private_helper would allow main to call it.