English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Cargo is the build system and package manager for Rust.
Rust developers commonly use Cargo to manage Rust projects and obtain the libraries required by the projects. In the previous tutorial, we used the cargo new greeting command to create a project named greeting. Cargo created a folder named greeting and deployed a typical file structure of a Rust project. This greeting folder is the project itself.
Cargo, in addition to creating projects, also has a series of functions such as building (build) and running (run) projects, which correspond to the following commands:
cargo build cargo run
Cargo also has functions such as package retrieval, packaging, and advanced building, for detailed usage methods, see the Cargo command.
Cargo is a good build tool, and if VSCode is integrated with it, VSCode will be a very convenient development environment.
In the previous chapter, we established the greeting project. Now, we use VSCode to open the greeting folder (Note that it is not w3codebox-greeting)。
After opening greeting, create a new folder .vscode (note the dot before vscode, if this folder already exists, there is no need to create a new one). Inside the newly created .vscode folder, create two files tasks.json and launch.json, with the following content:
{ "version": "2.0.0", "tasks": [ { "label": "build", "type": "shell", "command":"cargo", "args": ["build"] } ] }
{ "version": "0.2.0", "configurations": [ { "name": "(Windows) Start", "preLaunchTask": "build", "type": "cppvsdbg", "request": "launch", "program": "${workspaceFolder}/target/debug/${workspaceFolderBasename}.exe" "args": [], "stopAtEntry": false, "cwd": "${workspaceFolder}", "environment": [], "console": false } { "name": "(gdb) Start", "type": "cppdbg", "request": "launch", "program": "${workspaceFolder}/target/debug/${workspaceFolderBasename}.exe" "args": [], "stopAtEntry": false, "cwd": "${workspaceFolder}", "environment": [], "console": false, "MIMode": "gdb", "miDebuggerPath": "Enter the directory where GDB is located", "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) Start", "type": "cppdbg", "preLaunchTask": "build", "request": "launch", "program": "${workspaceFolder}/target/debug/${workspaceFolderBasename} "args": [], "stopAtEntry": false, "cwd": "${workspaceFolder}", "environment": [], "externalConsole": false, "MIMode": "lldb" } ] }
Then click on "Run" in the left column of VSCode.
If you are using MSVC, select "(Windows) Start".
If you are using MinGW and have installed GDB, select "(gdb) Start", and please fill in the "miDebuggerPath" in the launch.json before starting gdb.
The program will then start debugging and running. The output will appear in the "Debug Console":
The methods for debugging programs are similar to other environments, you just need to click on the red dot on the left side of the line number to set a breakpoint. When a breakpoint is encountered during execution, the program will pause so that developers can monitor the real-time values of variables.