Crates, modules and cargo test
Learn Rust · Beginner · 15 min · After Iterators and closures in Rust
How do you organise a Rust project and prove it works?
1Guess
mod count { pub fn words(s: &str) -> usize { s.split_whitespace().count() } } fn main() { println!("{}", count::words("one two three")); println!("{}", count::words(" spaced out ")); println!("{}", count::words("one,two")); }2Read
Packages, crates and modules
A package has a
Cargo.toml. It holds crates: a binary crate starts atsrc/main.rs, a library crate atsrc/lib.rs. Inside a crate,moddeclares a module, and its items are private until you mark thempub.usebrings a path into scope, socount::wordsbecomeswords.cargo add <name>adds a dependency from crates.io.3Code
A module and use
1 / 2mod count { pub fn words(s: &str) -> usize { s.split_whitespace().count() } pub fn lines(s: &str) -> usize { s.lines().count() } } use count::{lines, words}; const TEXT: &str = "first line\nsecond line here\nthird\n"; fn main() { println!("lines {}", lines(TEXT)); println!("words {}", words(TEXT)); println!("bytes {}", TEXT.len()); }Read the program. What will it print?
4Read
Tests with cargo test
Unit tests sit next to the code, in a module marked
#[cfg(test)]. A function marked#[test]is one test, and it fails if it panics, for example whenassert_eq!finds two different values.cargo testbuilds and runs them all. Integration tests live in atests/folder and use your crate like an outside user would.5Code
Unit tests in src/lib.rs
1 / 2pub fn words(s: &str) -> usize { s.split_whitespace().count() } #[cfg(test)] mod tests { use super::*; #[test] fn counts_words() { assert_eq!(words("one two three"), 3); } #[test] fn ignores_extra_spaces() { assert_eq!(words(" spaced out "), 2); } #[test] fn empty_text_has_no_words() { assert_eq!(words(""), 0); } }Read the program. What will it print?
6Fix
mod count { fn words(s: &str) -> usize { s.split_whitespace().count() } } fn main() { println!("{}", count::words("one two three")); }7Say it back
8Mission · 20 min
Build wc-lite, a tiny word counter. Put pub fn words and pub fn lines and a tests module in src/lib.rs. In src/main.rs read the file named by the first argument and print its lines, words and bytes, or a clear message when the argument is missing.
cargo new wc-lite && cd wc-lite cargo test cargo run -- Cargo.toml
Free AI coding tools to practise with
- AgentRouter Free Credits
Free AI coding credits: $25 per daily check-in and $50 per invite
No cardVerified
- Cline Free Models
Try select models free in Cline via rotating promotions with a usage quota
No cardVerified
Common questions
- What is the difference between a crate and a package?
- A crate is one unit of compilation: a binary with a main function, or a library. A package is what cargo manages: it has a Cargo.toml and holds one or more crates. cargo new creates a package with a binary crate, and cargo new --lib creates one with a library crate.
- How do I add a dependency to a Rust project?
- Run cargo add followed by the crate name inside your project. Cargo finds it on crates.io and writes it into Cargo.toml under dependencies. The next cargo build downloads and compiles it. Then use its items with a use line, for example use rand::Rng. You can also edit Cargo.toml by hand.
- Where do I put tests in Rust?
- Unit tests go in the same file as the code, inside a module marked #[cfg(test)], with each test marked #[test]. Integration tests go in a tests folder next to src and can only use your public API. Run both kinds with cargo test. A test that panics fails, and assert_eq! panics when the values differ.
Sources
- https://doc.rust-lang.org/book/ch07-02-defining-modules-to-control-scope-and-privacy.html (read 2026-12-03)
- https://doc.rust-lang.org/book/ch11-01-writing-tests.html (read 2026-12-03)
- https://doc.rust-lang.org/cargo/commands/cargo-test.html (read 2026-12-03)
Measured with rustc 1.100.1 (play.rust-lang.org, stable, edition 2024) on 2026-12-03
Published Dec 3, 2026 · Updated Dec 3, 2026