Skip to main content
Module 9 · Lesson 1 of 318/24 in the course~18 min
Module lessons (1/3)

Mod Modules, Path crate:: and Visibility pub(crate)

Lesson preview5 hands-on exercises readyCourse Rust · Module 9 — Modules, Cargo and Testing

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) and pub(super).
  • Import symbols with use, resolve conflicts with as, and design clean APIs with pub 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

  1. 11. Define modules mod and path crate:: / super::
  2. 22. Visibility levels: pub, pub(crate) and fields of struct
  3. 33. Separate files, use ... as and pub use
  4. 4Practical exercises
  5. 5Check for understanding
Pro Lesson · Module 9 — Modules, Cargo and Testing

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.