Module lessons (1/3)
Mod Modules, Path crate:: and Visibility pub(crate)
Structure binaries (main.rs) and libraries (lib.rs), export API with pub, pub(crate), use ... as and re-exports pub use.
What you'll learn in this lesson
- Organize scalable code with the module tree (
mod) and the private by default principle. - Control the exposure perimeter via
pub,pub(crate)andpub(super). - Import symbols with
use, resolve conflicts withas, and design clean APIs withpub use.
Overview
In Rust, every element (function, struct, field, module, constant) is private by default. Child modules can see the parent's private elements, while the parent cannot peek into a child module without an explicit pub modifier.
Marking pub struct User makes only the type name public, while all fields remain private until you declare pub even on the exposed fields! If you want to share a helper between modules in your crate without exposing it to external users, use pub(crate):
Sections covered in this lesson
- 11. Define modules
modand pathcrate::/super:: - 22. Visibility levels:
pub,pub(crate)and fields ofstruct - 33. Separate files,
use ... asandpub use - 4Practical exercises
- 5Check for understanding
Unlock "Mod Modules, Path crate:: and Visibility pub(crate)" and the full Course Rust curriculum
Structure binaries (`main.rs`) and libraries (`lib.rs`), export API with `pub`, `pub(crate)`, `use ... as` and re-exports `pub use`.
Unlock all 5 hands-on exercises in this lesson plus every advanced WebAssembly module with automated checks.
- Instant access to all advanced modules in Course Rust plus 17 more courses
- Hands-on browser exercises with automated test suites and solutions
- 14-day money-back guarantee processed via Paddle.com
14-day guarantee · Cancel anytimeModule 1, all 18 interactive games, and cheatsheets are always free.