Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Cargo Tutorial

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:

Cargo's Main Features and Functions:

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:

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.