Cargo is Rust's official package manager and build tool, which simplifies project creation, dependency management, and compilation processes. Through Cargo, you can easily manage Rust projects, from simple scripts to complex applications.
Installing and Verifying Cargo
When you install Rust, Cargo is usually included automatically. To check if Cargo is installed, you can run the following command in the terminal:
cargo --version
cargo 1.70.0 (ec8a8a0ca 2023-04-25)
If you see a version output similar to the above, it means Cargo is correctly installed.
Creating a New Project
Using Cargo to create a new project is very simple. Run the following command to create a new project named my_project:
cargo new my_project
This will generate a folder named my_project in the current directory, containing the basic project structure. Enter the project directory and view the files:
cd my_project
ls -la
Cargo.toml src/ .gitignore
Key files include:
Cargo.toml: The project's configuration file, used to define metadata and dependencies.src/main.rs: The main program file, containing a simpleHello, world!example.
Understanding the Cargo.toml File
Cargo.toml is the core configuration file for Cargo projects. Open it, and you will see content similar to the following:
[package]
name = "my_project"
version = "0.1.0"
edition = "2021"
[dependencies]
This file uses TOML format, where:
- The
[package]section defines basic project information, such as name, version, and Rust edition. - The
[dependencies]section is used to list the packages the project depends on.
Note: The edition field specifies the Rust edition, such as 2021, which affects language features and compatibility.
Adding and Managing Dependencies
To add dependencies to the project, simply specify the package name and version in the [dependencies] section of Cargo.toml. For example, to add a popular logging library log:
[dependencies]
log = "0.4"
Then run cargo build to download and compile the dependencies:
cargo build
Cargo will automatically download packages from crates.io and handle version resolution. You can use dependencies in your code, for example:
use log::{info, warn};
fn main() {
info!("Application started");
warn!("This is a warning message");
}
Building and Running Projects
Cargo provides several common commands for building and running projects. To compile the project, use:
cargo build
This will generate an executable in the target/debug directory. To run the project directly, use:
cargo run
Hello, world!
For release builds, add the --release flag to optimize performance:
cargo build --release
cargo run --release
Common Cargo Commands Reference
The table below summarizes the basic Cargo commands and their purposes:
| Command | Description |
|---|---|
cargo new |
Create a new project |
cargo build |
Compile the project |
cargo run |
Compile and run the project |
cargo check |
Quickly check for code errors |
cargo test |
Run tests |
cargo update |
Update dependency versions |
Note: After modifying Cargo.toml, remember to run cargo build or cargo update to apply the changes, otherwise dependencies may not take effect.
Project Structure
A typical Cargo project structure is as follows:
my_project/ ├── Cargo.toml ├── src/ │ ├── main.rs │ └── lib.rs (optional) ├── target/ └── .gitignore