Skip to main content Free coding courses, practical tutorials, and real projects from BraveProgrammer. Learn web development with React, Next.js, and TypeScript.
Navigation Resources
© 2026 BraveProgrammer. All rights reserved.
Understanding Cargo Projects | Rust Fundamentals lesson | BraveProgrammer
Courses / Rust Fundamentals
Understanding Cargo Projects 15 minutes
Understanding Cargo Projects
Cargo is Rust's package manager and build system. It handles compilation, dependencies, tests, documentation, and publishing. Understanding its structure is essential for effective Rust development.
Project Structure
my_project/
├── Cargo.toml # Project manifest
├── Cargo.lock # Reproducible builds lock file
├── src/
│ ├── main.rs # Binary entry point
│ ├── lib.rs # Library entry point
│ └── bin/ # Additional binaries
├── tests/ # Integration tests
├── benches/ # Performance benchmarks
├── examples/ # Example programs
└── README.md # Documentation
The Cargo.toml Manifest Your project configuration file:
[package]
name = \"hello_world\"
version = \"0.1.0\"
edition = \"2021\" # 2015, 2018, or 2021
authors = [\"Name <email>\"]
description = \"A sample program\"
license = \"MIT\"
[dependencies]
# Production dependencies
serde = \"1.0\"
tokio = { version = \"1.35\", features = [\"full\"] }
[dev-dependencies]
# Test-only dependencies
proptest = \"1.0\"
[profile.release]
opt-level = 3 # Maximum optimization
lto = true # Link-time optimization
strip = true # Strip debug symbols
Crate Types
Binary Crates (src/main.rs) Produces executable programs:
Library Crates (src/lib.rs)
pub fn greet (name: &str ) -> String {
format! (\"Hello, {}!\", name)
}
Mixed Projects Both binary and library in one project:
pub fn add (a: i32 , b: i32 ) -> i32 { a + b }
use my_project::add;
fn main () { println! (\"{}\", add(2, 3)); }
Essential Commands Command Purpose cargo buildCompile in debug mode cargo build --releaseCompile with optimizations cargo runCompile and run cargo checkVerify without building cargo testRun all tests cargo fmtFormat code cargo clippyLint your code cargo doc --openGenerate and open docs cargo cleanRemove build artifacts
Managing Dependencies Add via command line or edit Cargo.toml:
cargo add serde
cargo add --dev proptest
cargo add tokio --features full
serde = \"1.0\" # Exact version
tokio = \"~1.35\" # Patch updates only
chrono = \"^0.4\" # Up to next major version
The Cargo.lock File Locks dependencies to specific versions for reproducible builds. Commit to version control for binary projects, exclude for libraries.
Make it stick Practice what you've learned 1 quiz · 1 practical test · Optional activities from Rust Fundamentals Browse activities Quiz 1 Course Quiz 3 questions · Pass mark: 70% Start quiz Practical test 1 Hello Rust Program Create a simple Rust program that prints "Hello, Rust!" Start practical test