1. What is Cargo?
Cargo is Rust's official package manager and build system.
It helps developers create, build, run, test, and manage Rust projects.
With Cargo, you can:
- Create new Rust projects.
- Compile Rust source code.
- Run Rust applications.
- Add external libraries.
- Manage project dependencies.
- Run automated tests.
- Build optimized applications.
- Organize large projects.
Important Cargo Commands
Command | Purpose |
|---|---|
| Create a new project |
| Initialize Cargo in an existing directory |
| Compile a project |
| Compile and execute a project |
| Check code without producing an executable |
| Run tests |
| Add a dependency |
| Update dependencies within version requirements |
| Remove generated build files |
| Generate documentation |
| Display dependency relationships |
2. Create a Rust Project
Open your terminal and execute:
Cargo creates a new project.
Now open it:
Default Project Structure
Let's understand these files and folders.
File / Folder | Purpose |
|---|---|
| Project configuration and dependencies |
| Contains Rust source code |
| Application entry point |
| Files Git should ignore |
3. Understanding Cargo.toml
Cargo.toml is the main configuration file of a Rust project.
Example:
Explanation
[package]
Contains project information.
name
Defines the package name.
version
Defines the current package version.
edition
Defines the Rust language edition used by the project.
[dependencies]
Contains external libraries required by the project.
For example:
Now your project can use Serde and serde_json.
4. What is a Crate?
A crate is the basic unit of compilation and distribution in Rust.
There are two main types of crates:
1. Binary Crate
A binary crate produces an executable application.
Example:
The main.rs file is commonly used as the entry point of a binary crate.
2. Library Crate
A library crate provides reusable code that other programs can use.
It usually starts from:
Example:
Difference
Binary Crate | Library Crate |
|---|---|
Produces an executable | Provides reusable code |
Common entry point: | Common entry point: |
Used to run applications | Used by other crates |
A single Cargo package can contain both a library crate and one or more binary crates.
5. What is a Package?
A package is a collection of one or more crates managed by Cargo.
A package contains a Cargo.toml file.
Example:
This package contains:
- One binary crate.
- One library crate.
Remember:
- Package: A Cargo-managed project.
- Crate: A unit of compilation.
- Module: A way to organize code inside a crate.
6. Understanding Modules
Modules help organize Rust code into smaller, manageable sections.
Suppose you have a student management application.
Instead of writing everything in one file, you can organize code like this:
student.rs
fees.rs
main.rs
Output:
Here:
mod student;declares the student module.mod fees;declares the fees module.pubmakes the functions accessible outside their defining module.student::get_student()calls a function using its module path.
7. Organize Modules into Folders
For larger applications, modules can be organized into folders.
Example:
student/profile.rs
student/mod.rs
fees/calculation.rs
fees/mod.rs
main.rs
Output:
Note: Modern Rust projects can also use the folder-based module convention without mod.rs, by placing student.rs beside a student/ directory. Both approaches are supported.
8. What is a Dependency?
A dependency is an external library that your project uses.
For example, if you want to work with JSON, you can use the serde_json dependency.
Add it using:
Cargo updates your Cargo.toml file.
Example:
Now use it in your Rust program:
9. What is Cargo.lock?
When you add dependencies, Cargo creates a file named:
It records the exact dependency versions selected for your project.
Why is this useful?
Suppose your project uses:
This allows compatible releases within the specified version requirement.
But the exact version selected for your build is recorded in Cargo.lock.
This helps make builds more reproducible.
Important:
Cargo.tomldescribes dependency requirements.Cargo.lockrecords the resolved dependency versions.
For application projects, you should generally commit Cargo.lock to version control.
10. Understanding Cargo Build
Cargo provides different ways to compile your application.
Development Build
Creates a development build.
The executable is usually generated inside:
Run the Application
Compiles and executes the application.
Release Build
Creates an optimized build.
The executable is generated inside:
Difference
Development Build | Release Build |
|---|---|
|
|
Faster compilation | More optimization |
Useful during development | Useful for production |
Usually larger runtime overhead | Usually faster execution |
11. What is Cargo Check?
The command:
Checks your Rust code for compilation errors without producing a final executable.
It is generally faster than a full build.
Use it frequently while developing your application.
12. What is Cargo Tree?
When your project contains many dependencies, it can be difficult to understand which libraries are being used.
Run:
It displays the dependency tree.
Example:
This helps you understand direct and indirect dependencies.
13. What are Cargo Features?
Some Rust libraries provide optional features.
For example:
This enables selected Tokio features.
You can also define dependencies manually:
Features help you enable only the functionality your application needs.
14. What is a Cargo Workspace?
A workspace allows multiple related Rust packages to be managed together.
This is useful for larger projects.
Suppose you are building a school management system with separate packages for:
- Student management.
- Fee management.
- Attendance management.
- Main application.
You can organize them in a workspace.
Workspace Structure
Root Cargo.toml
This tells Cargo that the three packages belong to one workspace.
Why Use a Workspace?
- Manage multiple related packages.
- Share a common build directory.
- Run tests across packages.
- Manage dependencies centrally when needed.
- Keep large applications organized.
15. Cargo Profiles
Cargo supports different build profiles.
The most common profiles are:
Profile | Purpose |
|---|---|
| Development |
| Production |
| Testing |
| Benchmarking |
You can customize profiles in Cargo.toml.
Example:
opt-level controls optimization level. Higher optimization can improve runtime performance but may increase compilation time.
16. Useful Cargo Commands
Command | Description |
|---|---|
| Create a new application |
| Initialize a project in the current directory |
| Compile the project |
| Compile and run |
| Check compilation errors |
| Run tests |
| Add a dependency |
| Remove a dependency |
| Update dependency resolution |
| Display dependencies |
| Remove build artifacts |
| Generate and open documentation |
| Build an optimized application |
17. Mini Project: Student Management Package
Let's organize a small student management application using modules.
Project Structure
student.rs
fees.rs
main.rs
Output
This example demonstrates how to separate application responsibilities into different modules.
18. Practice Exercises
- Create a new Cargo project.
- Understand the
Cargo.tomlfile. - Add the
serde_jsondependency. - Create a separate module named
student. - Create another module named
fees. - Call functions from both modules in
main.rs. - Run
cargo check. - Run
cargo build --release. - Execute
cargo tree. - Create a workspace containing two small packages.