Packages, Crates, and Modules
As your programs grow larger, you'll find it essential to organize your code into logical units. Oxide (like Rust) provides a powerful module system that lets you:
- Break your code into reusable pieces
- Keep related functionality together
- Control which parts of your code are public and which are private
- Avoid naming conflicts
In this chapter, we'll explore:
- Packages and crates: The containers for your code
- Modules: How to organize code within crates
- import statements: How to bring names into scope
- Visibility: Controlling what parts of your API are public
These features form the foundation of larger Oxide projects and libraries. Whether you're organizing a single crate or distributing code across multiple crates, mastering modules will make your code more maintainable and your intentions clearer.
Overview
Before diving into syntax, let's clarify the key concepts:
- Package: A Cargo feature that contains one or more crates
- Crate: A tree of modules that produces a library or executable
- Module: A way to organize code into logical hierarchies with control over privacy
- Path: A way to name an item in the module tree (e.g.,
restaurant.food.appetizers.Appetizer)
We'll start with packages and crates, then move to modules and visibility, and finally explore how to construct paths to access items.
Let's begin!