Cargo Tutorial
What is Cargo
Cargo is Rust's build system and package manager.
Rust developers commonly use Cargo to manage Rust projects and acquire libraries that the project depends on. In the previous tutorial, we used the cargo new greeting
command to create a project named greeting. Cargo created a folder named greeting and set up the typical file structure for a Rust project inside it. This greeting folder is the project itself.
Cargo Features
Besides creating projects, Cargo also has capabilities to build and run projects, among other functions. The commands for building and running are as follows:
cargo build
cargo run
Cargo also supports package fetching, packaging, and advanced build functionalities. For detailed usage, refer to the Cargo commands.
Configuring Rust Projects in VSCode
Cargo is a great build tool, and when combined with VSCode, it makes VSCode a very convenient development environment.
In the previous chapter, we created the greeting project. Now, we will open the greeting folder (note: not tutorialpro-greeting) in VSCode.
After opening greeting, create a new folder named .vscode
(note the dot before vscode; no need to create it if it already exists). Inside the .vscode folder, create two files: tasks.json
and launch.json
. The contents of these files are as follows:
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": "path_to_gdb_here",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
]
}
{
"version": "0.2.0",
"configurations": [
{
"name": "Debug",
"type": "gdb",
"preLaunchTask": "build",
"request": "launch",
"target": "${workspaceFolder}/target/debug/${workspaceFolderBasename}",
"cwd": "${workspaceFolder}"
}
]
}
{
"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" on the left sidebar in VSCode.
If you are using MSVC, choose "(Windows) Launch".
If you are using MinGW and have installed GDB, choose "(gdb) Launch", and make sure to fill in the "miDebuggerPath" in launch.json before launching gdb.
The program will start debugging. The output will appear in the "Debug Console":
Debugging Rust in VSCode
The method to debug the program is similar to other environments. You can set breakpoints by clicking the red dot next to the line numbers. The program will pause at the breakpoint, allowing the developer to monitor the values of variables in real time. ```