Dividing Your Code Into Modules

You can write huge programs in one file, but dividing your code into smaller sections—known as modules in Rust—offers significant advantages:

  • It’s a lot easier to find your Map functionality in a file named map.rs than it is to remember that it’s somewhere around line 500 of an ever-growing main.rs.

  • Cargo can compile modules concurrently, leading to much better compilation times.

  • Bugs are easier to find in self-contained code that limits linkages to other sections of code.

Modules may either be a single rs file or a directory. In this chapter, you’ll use single file modules. You’ll learn to create directory-based modules in Multi-File Modules.

Crates and Modules

Rust programs are divided into crates and modules ...

Get Hands-on Rust now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.