Modules, Cargo and Testing in Rust
by Hexagon, 3 min read rust guide
So far, everything has lived in a single file. Real projects need more structure. In this article, we'll split code into modules, add crates from crates.io, and write tests, all with the tools that come with Rust.
Modules
A module groups related code, and decides what's visible from the outside. You
can declare a module directly in a file, with mod:
Everything is private by default. pub makes an item visible outside the
module. Note that for structs, each field needs its own pub. That lets you
make a struct public, while keeping its fields private and only allowing
changes through methods.
use
Writing geometry::Point everywhere gets long. use brings names into scope:
use Point;
use ; // Several names at once
This is like import in JavaScript, but use only creates a shortcut. The
module itself is found through mod, not through use.
Modules in files
In a real project, each module usually lives in its own file. With this layout:
src/
├── main.rs
├── geometry.rs
└── network/
├── mod.rs
└── client.rs
main.rs declares the modules, and Rust finds the files by their names:
// src/main.rs
// Loads src/geometry.rs
// Loads src/network/mod.rs
use Point;
The content of geometry.rs is the same as what was inside mod geometry { }
above, without the braces. And network/mod.rs would contain
pub mod client;, to load network/client.rs. Instead of network/mod.rs,
you can also name the file network.rs, next to the network folder.
Inside a module, crate:: refers to the root of your project, and super::
to the parent module.
Binaries and Libraries
A Cargo project, called a package, can contain a binary, a library, or both:
src/main.rsis the start of a program you can run.src/lib.rsis the start of a library, that other code can use.
A common pattern is to put most of the code in lib.rs, and keep main.rs
small. Create a library project with cargo new --lib my_library.
Cargo.toml and Crates
Cargo.toml describes your package:
[]
= "hello"
= "0.1.0"
= "2024"
[]
edition decides which version of the language rules to use. Editions come
every few years, and let Rust make changes without breaking old code. Crates
from different editions work fine together.
Adding dependencies
Packages from crates.io are called crates. Let's add Croner, a cron pattern parser, and chrono, a date and time library:
This adds them to Cargo.toml:
[]
= "0.4"
= "5"
The next cargo build or cargo run downloads and compiles them. Now they can
be used like any module:
// Needs croner and chrono in Cargo.toml
use Utc;
use Cron;
use FromStr;
The versions in Cargo.toml follow semantic versioning. "5" means any 5.x.y
version, as those should be compatible. The exact versions that were picked are
saved in Cargo.lock. Commit that file for programs, so everyone builds with
the same versions.
To find crates, search crates.io, or lib.rs, which is a bit easier to browse. Documentation for every published crate is available at docs.rs.
Testing
Tests are built into Rust and Cargo. A test is a function marked with
#[test]. By convention, unit tests live in a tests module at the bottom of
the same file as the code they test:
// src/lib.rs, run the tests with: cargo test
#[cfg(test)] means that the module is only compiled when running tests, so it
doesn't end up in your program. Since the tests module is inside the same file,
it can test private functions too.
Run all tests with:
The most common checks are:
assert!(condition)fails if the condition is false.assert_eq!(a, b)fails ifa != b, and shows both values.assert_ne!(a, b)fails ifa == b.
All of them take an optional message, like
assert!(list.is_empty(), "list should be empty, but was {list:?}").
Tests that use your library from the outside, like a user would, go in a
tests/ folder next to src/. Each file there is compiled as its own crate.
Documentation tests
Code examples in documentation comments are tested too:
// In src/lib.rs of a crate called my_crate
/// Adds two numbers.
///
/// ```
/// assert_eq!(my_crate::add(2, 2), 4);
/// ```
cargo test runs the example, so your documentation can never get out of date
without you noticing. Run cargo doc --open to see the documentation in your
browser.
More Cargo Tools
A few more commands that you'll use all the time:
cargo checkchecks that the code compiles, without building it. Much faster thancargo build.cargo fmtformats all your code in the standard style. No more discussions about where braces go.cargo clippyis a linter with hundreds of checks. It catches mistakes, and suggests simpler ways to write things. It's a great teacher when you're learning.cargo build --releasebuilds an optimized version of your program.cargo updateupdates dependencies to the newest compatible versions.
Conclusion
Cargo is one of the things Rust is most praised for. Building, testing,
formatting, linting, documentation and dependencies, all with one tool that
works the same on every computer. A good habit is to run cargo fmt,
cargo clippy and cargo test before every commit.
As an exercise, take the Student example from the article about enums, move
it into its own module, and write tests for average, including the case with
no scores.