Real Projects · 14 min · free with an account

Modules

Lesson preview · about 30%

Modules split a crate into files, and privacy shapes the surface.

Core idea

Items are private by default; pub is what exposes them.

Start with the idea

mod declares a module and use brings a path into scope, while pub decides what actually escapes. Cargo.toml describes the package around them — the same file the package manager reads to resolve your dependencies.

A module gives names and boundaries to related code. In the example, mod greeter; tells Rust to compile the module from src/greeter.rs; greeter::message() follows a path to an item inside it. The use keyword can shorten a path in the current scope, but it does not change whether an item is visible. Visibility starts private. pub opens an item to callers permitted by the module hierarchy, and a public function exposed from a library crate becomes part of the interface other crates may rely on.

Start a tiny project with src/main.rs and src/greeter.rs.

Keep learning, for free

The rest of this lesson is waiting for you.

Create a free account to read the complete explanation, see the infographic, explore the code, and try the exercises. All 742 lessons and algorithm steps are included.

No payment details. Your account opens the complete Academy.

← All lessons in Cargo & Real Projects