Cargo Tutorial
In Rust development, almost all projects are managed and built with Cargo, because it provides a convenient workflow and powerful features, making Rust development more efficient and reliable.
What is Cargo
Cargo is Rust's official build system and package manager. It has two main roles:
It mainly has two roles:
-
Project management: Cargo is used to create, build, and manage Rust projects. With Cargo, you can easily create new projects, manage project dependencies, and perform operations such as building, running, and testing projects.
-
Package manager: Cargo also acts as Rust's package manager. It allows developers to introduce and manage dependencies (such as third-party libraries) in a project and ensures version management and compatibility of these dependencies.
Cargo's Main Features and Functions:
-
Dependency Management: Cargo manages a project's dependencies through the
Cargo.tomlfile, which lists all external libraries required by the project and their versions. -
Build system: Cargo uses the Rust compiler (rustc) to build projects, automatically handling the compilation and linking of dependencies.
-
Package registry: Cargo interacts with crates.io , this Rust community package registry, allowing developers to search for, add, and manage third-party libraries.
-
Build configuration: Through
Cargo.tomlandCargo.lockfile, Cargo allows developers to configure build options such as compiler options, features, and target platforms. -
Project templates: Cargo provides templates for creating new projects, which can be
cargo newcommand to quickly start a new project. -
Testing: Cargo provides a simple command
cargo testto run the project's unit tests. -
Benchmarking: Cargo supports using the
cargo benchcommand for benchmarking. -
Release: Through
cargo publishcommand, developers can publish their libraries to crates.io for other developers to use. -
Custom build scripts: Cargo allows the use of custom build scripts to handle more complex build needs.
-
Multi-target projects: Cargo supports defining multiple targets in one project, such as executables, libraries, tests, and benchmarks.
-
Cross-platform building: Cargo supports building Rust programs across multiple platforms, including Windows, macOS, Linux, and various embedded systems.
-
Build cache: To speed up builds, Cargo uses a build cache to store compiled dependencies.
-
Offline Work: Cargo supports working without an internet connection, automatically using locally cached dependencies.
-
Plugin system: Cargo allows developers to write plugins to extend its functionality.
-
Environment Variables: Cargo supports overriding default build and run behavior through environment variables.
Cargo Functions
In addition to creating projects, Cargo also has a series of functions such as building (build) and running (run) projects; building and running correspond to the following commands:
cargo new: Create a new Rust project.cargo build: Compile the current project.cargo run: Compile and run the current project.cargo check: Check the current project for syntax and type errors.cargo test: Run the current project's unit tests.cargo update: Update dependencies specified in Cargo.toml to their latest versions.cargo --help: View Cargo's help information.cargo publish: Publish a Rust project to crates.io.cargo clean: Clean up temporary files and directories generated during the build.
Configuring Rust Projects in VSCode
Cargo is a good build tool; if VSCode is used together with it, VSCode will be a very convenient development environment.
In the previous chapter we created the greeting project; now we open the greeting folder with VSCode (note: not runoob-greeting)。
After opening greeting, create a new folder .vscode inside it (note the dot before vscode; if this folder already exists, there is no need to create it). In the new .vscode folder, create two files, tasks.json and launch.json, with the following contents:
tasks.json file
{
"
version
"
:
"
2.0.0
"
,
"
tasks
"
:
[
{
"
label
"
:
"
build
"
,
"
type
"
:
"
shell
"
,
"
command
"
:
"
cargo
"
,
"
args
"
:
[
"
build
"
]
}
]
}
launch.json file (for Windows systems)
{
"
version
"
:
"
0.2.0
"
,
"
configurations
"
:
[
{
"
name
"
:
"
(Windows) Launch
"
,
"
preLaunchTask
"
:
"
build
"
,
"
type
"
:
"
cppvsdbg
"
,
"
request
"
:
"
launch
"
,
"
program
"
:
"
${workspaceFolder}/target/debug/${workspaceFolderBasename}.exe
"
,
"
args
"
:
[
]
,
"
stopAtEntry
"
:
false
,
"
cwd
"
:
"
${workspaceFolder}
"
,
"
environment
"
:
[
]
,
"
externalConsole
"
:
false
}
,
{
"
name
"
:
"
(gdb) Launch
"
,
"
type
"
:
"
cppdbg
"
,
"
request
"
:
"
launch
"
,
"
program
"
:
"
${workspaceFolder}/target/debug/${workspaceFolderBasename}.exe
"
,
"
args
"
:
[
]
,
"
stopAtEntry
"
:
false
,
"
cwd
"
:
"
${workspaceFolder}
"
,
"
environment
"
:
[
]
,
"
externalConsole
"
:
false
,
"
MIMode
"
:
"
gdb
"
,
"
miDebuggerPath
"
:
"
Enter the directory where GDB is located here
"
,
"
setupCommands
"
:
[
{
"
description
"
:
"
Enable pretty printing for gdb
"
,
"
text
"
:
"
-enable-pretty-printing
"
,
"
ignoreFailures
"
:
true
}
]
}
]
}
launch.json file (for Linux systems)
{
"
version
"
:
"
0.2.0
"
,
"
configurations
"
:
[
{
"
name
"
:
"
Debug
"
,
"
type
"
:
"
gdb
"
,
"
preLaunchTask
"
:
"
build
"
,
"
request
"
:
"
launch
"
,
"
target
"
:
"
${workspaceFolder}/target/debug/${workspaceFolderBasename}
"
,
"
cwd
"
:
"
${workspaceFolder}
"
}
]
}
launch.json file (for Mac OS systems)
{
"
version
"
:
"
0.2.0
"
,
"
configurations
"
:
[
{
"
name
"
:
"
(lldb) Launch
"
,
"
type
"
:
"
cppdbg
"
,
"
preLaunchTask
"
:
"
build
"
,
"
request
"
:
"
launch
"
,
"
program
"
:
"
${workspaceFolder}/target/debug/${workspaceFolderBasename}
"
,
"
args
"
:
[
]
,
"
stopAtEntry
"
:
false
,
"
cwd
"
:
"
${workspaceFolder}
"
,
"
environment
"
:
[
]
,
"
externalConsole
"
:
false
,
"
MIMode
"
:
"
lldb
"
}
]
}
Then click "Run" in the left column of VSCode.
If you are using MSVC, select "(Windows) Launch".
If you are using MinGW and have GDB installed, select "(gdb) Launch". Before starting gdb, please note to fill in "miDebuggerPath" in launch.json.

The program will start running in debug mode. The output will appear in the "Debug Console":

Debugging Rust in VSCode
The method of debugging a program is similar to other environments. You only need to click the red dot to the left of a line number to set a breakpoint. When a breakpoint is encountered during execution, it will pause so developers can monitor real-time variable values.
