Crates and the Crate Root
Project structure.
Crates and the Crate Root is a free Learn Rust Coding lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Learn Rust Coding learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
What Is a Crate?
A crate is the smallest unit the Rust compiler works with. There are two kinds: a binary crate that compiles to an executable, and a library crate that other code depends on.
Binary vs Library
A binary crate has a main function and produces a program you can run. A library crate has no main; it exposes reusable items for other crates.
- Binary root:
src/main.rs - Library root:
src/lib.rs
The Crate Root
The crate root is the source file the compiler starts from. It is the top of the module tree, and the keyword crate in a path refers to it.
fn main() {
crate::greet();
}
fn greet() {
println!("hello from the crate root");
}Cargo and Packages
A package is one or more crates managed by Cargo, described in Cargo.toml. A package contains at most one library crate and any number of binary crates.
Cargo handles building, testing, and dependencies.
A Minimal Cargo.toml
The manifest declares the package name, version, and dependencies. Cargo reads it to know how to build your project.
[package]— name, version, edition[dependencies]— external crates
Standard Project Layout
Cargo expects a conventional structure:
src/main.rs— binary crate rootsrc/lib.rs— library crate rootsrc/bin/— extra binariestests/— integration tests
Following the convention means zero configuration.
Modules Live Under the Root
The crate root can declare modules, which form the module tree. Everything you build hangs off this single root file.
mod utils {
pub fn shout(s: &str) -> String {
s.to_uppercase()
}
}
fn main() {
println!("{}", utils::shout("crate"));
}Splitting Into Files
When the root declares mod utils; with a semicolon, Cargo loads src/utils.rs. The file's contents become that module's body, keeping a single tree across many files.
Using a Library From a Binary
In a package with both lib.rs and main.rs, the binary refers to the library by the package name. The library exposes pub items the binary consumes.
This split keeps logic in the library and the entry point thin.
Adding Dependencies
Run cargo add rand or edit [dependencies] in Cargo.toml. Cargo downloads the crate from crates.io and makes it available to use.
Cargo also pins exact versions in Cargo.lock for reproducible builds.
The Big Picture
From small to large: module < crate < package. Modules organize a crate; the crate root anchors the tree; the package and Cargo tie it all together.
Quick Check
Test your project structure knowledge.
Recap
You learned project structure:
- A crate is a binary or a library
- The crate root (
main.rsorlib.rs) anchors the module tree - A package groups crates and is described by
Cargo.toml - Cargo manages layout, builds, and dependencies
Frequently asked questions
Is the “Crates and the Crate Root” lesson free?
Yes — the full text of “Crates and the Crate Root” is free to read here on the web, and the Learn Rust Coding course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Learn Rust Coding course, upgrade to CoddyKit PRO.
What will I learn in “Crates and the Crate Root”?
Project structure. You practise Learn Rust Coding with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Learn Rust Coding?
No prior experience is required. Learn Rust Coding on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Crates and the Crate Root” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Learn Rust Coding lesson?
Yes. Every Learn Rust Coding lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Modules and mod
- pub and Visibility
- use and Paths
- Crates and the Crate Root