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?

  1. 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"));
    }
    
    What does this program print?
  2. 2Read

    Packages, crates and modules

    A package has a Cargo.toml. It holds crates: a binary crate starts at src/main.rs, a library crate at src/lib.rs. Inside a crate, mod declares a module, and its items are private until you mark them pub. use brings a path into scope, so count::words becomes words. cargo add <name> adds a dependency from crates.io.

  3. 3Code

    A module and use

    1 / 2
    mod 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?

    Run it in the playground

  4. 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 when assert_eq! finds two different values. cargo test builds and runs them all. Integration tests live in a tests/ folder and use your crate like an outside user would.

  5. 5Code

    Unit tests in src/lib.rs

    1 / 2
    pub 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?

    Run it in the playground

  6. 6Fix

    mod count {
        fn words(s: &str) -> usize {
            s.split_whitespace().count()
        }
    }
    
    fn main() {
        println!("{}", count::words("one two three"));
    }
    
    This program does not compile. What is the bug?
  7. 7Say it back

  8. 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

  1. AgentRouter Free Credits

    Free AI coding credits: $25 per daily check-in and $50 per invite

    No cardVerified

  2. Cursor Free Plan

    Cursor Agent with limited agent and chat requests, $0/month

    No cardVerified

  3. Cline Free Models

    Try select models free in Cline via rotating promotions with a usage quota

    No cardVerified

All free ai coding tools →

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

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

Never miss new offers and news

New tools, credits, and AI news are added regularly. Pick how you want to hear about them.

Telegram channel

Join @aifree_dev and get new free AI offers and news right in your Telegram feed.

Browser notifications

Turn on push notifications and get an alert on this device whenever something new is posted. No account needed.

Email newsletter

A weekly digest of new, changed and expiring free AI offers. Double opt-in, one-click unsubscribe. See the latest issue.

Unsubscribe anytime. Privacy

Frequently asked questions

What is aifree.dev?

aifree.dev helps everyone access AI through a curated directory of free AI tools, API credits, free tiers, and startup programs for coding, image, voice, video, and students - verified regularly.

How are offers presented?

Each offer includes its access details, free models when available, and expiration information when provided. Limited-time deals stay visible so they are easy to compare.

Do education and startup programs need verification?

Yes. Providers usually ask students to verify their status with a school email, student ID, or a program like the GitHub Student Pack, and ask startups to verify company details such as age, funding, or website. Check each offer’s claim page for the exact requirements.

What kinds of free AI offers are listed?

Free tiers and free trials from AI providers, free API credits, startup and student programs, and free tools for coding, image generation, voice, and video.

How can I suggest a tool or report a broken link?

Use the submit form: paste the offer page URL and a short note. A person checks every submission before anything is listed. Broken links and changed terms go through the same form.

Know a good one?

Tell us about an AI tool worth knowing. Found a broken link? We want to hear that too.

Submit an offer