Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Crates and Modules

What you’ll learn: How Rust code organization maps to Java packages, modules, and artifacts.

Difficulty: 🟢 Beginner

Rust organizes code around crates and modules rather than packages and classpaths.

Mental Mapping

Java ideaRust idea
artifact or modulecrate
packagemodule tree
package-private or public APImodule privacy plus pub

Basic Layout

src/
├── main.rs
├── lib.rs
├── api.rs
└── model/
    └── user.rs

Visibility

  • items are private by default
  • pub exposes an item more broadly
  • pub(crate) exposes within the current crate

This default privacy is stricter than typical Java codebases and often leads to cleaner boundaries.

Guidance

  • keep module trees shallow at first
  • design crate boundaries around ownership of concepts, not around arbitrary layering
  • expose a small public API and keep the rest internal

Crates and modules are simpler than many Java build layouts, but they reward deliberate boundary design.