Skip to main content
Brave Programmer Logo

BraveProgrammer

BraveProgrammer

HomeProjectsBlogsCoursesLessonsAbout

Site footer

BraveProgrammer

Free coding courses, practical tutorials, and real projects from BraveProgrammer. Learn web development with React, Next.js, and TypeScript.

Navigation

  • Home
  • Projects
  • Blogs
  • Courses

Resources

  • About
  • Lessons
  • Teach on BraveProgrammer

© 2026 BraveProgrammer. All rights reserved.

Understanding Cargo Projects | Rust Fundamentals lesson | BraveProgrammer
  1. Courses
  2. /
  3. Rust Fundamentals

Lesson 4 of 50

·rust

Understanding Cargo Projects

15 minutes

Course progress8%

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:

$ cargo run

Library Crates (src/lib.rs)

Produces reusable code:

// src/lib.rs
pub fn greet(name: &str) -> String {
    format!(\"Hello, {}!\", name)
}

Mixed Projects

Both binary and library in one project:

// src/lib.rs
pub fn add(a: i32, b: i32) -> i32 { a + b }

// src/main.rs
use my_project::add;
fn main() { println!(\"{}\", add(2, 3)); }

Essential Commands

CommandPurpose
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

Version specifications:

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 stickPractice what you've learned1 quiz · 1 practical test · Optional activities from Rust FundamentalsBrowse activities
Previous lessonYour First Program (Hello World)Continue to lesson 5Variables and MutabilityBack to Rust Fundamentals curriculum