RUST入门:如何用vscode调试rust程序

RUST已经流行一阵子了,但是比较系统的IDE介绍还是比较少,这里我简单介绍 一下如何用vscode实现单步调试rust程序,就像我们平时调试c++程序一样。

学习资料网站

首先,介绍几个学习rust的好网站,

给vscode安装一些必要的插件

本人安装的插件包括

Even Better TOML

CodeLLDB

Rust analyzer

Tabnine

插件的功用直接到vscode里看介绍就可以了。

创建程序并开始调试

打开vscode,然后通过terminal->new terminal打开一个终端,

在终端输入

>> cargo new test001

创建一个rust项目,这里test001是我们的项目名称。

然后,用vscode打开这个文件夹,就可以看到项目中有一个src/main.rs文件,里面的代码是

rust 复制代码
fn main() {
    println!("Hello, world!");
}

在这一句打上断点。

然后,点击左侧的Run and Debug (Ctrl+Shift+D) 按钮,然后,会弹出一个大按钮,下面有一行小字:

Create a launch.json file

点击这行小字,vscode就会自动为你创建一个.vscode/launch.json文件,其内容如下,

rust 复制代码
{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "type": "lldb",
            "request": "launch",
            "name": "Debug executable 'test001'",
            "cargo": {
                "args": [
                    "build",
                    "--bin=test001",
                    "--package=test001"
                ],
                "filter": {
                    "name": "test001",
                    "kind": "bin"
                }
            },
            "args": [],
            "cwd": "${workspaceFolder}"
        },
        {
            "type": "lldb",
            "request": "launch",
            "name": "Debug unit tests in executable 'test001'",
            "cargo": {
                "args": [
                    "test",
                    "--no-run",
                    "--bin=test001",
                    "--package=test001"
                ],
                "filter": {
                    "name": "test001",
                    "kind": "bin"
                }
            },
            "args": [],
            "cwd": "${workspaceFolder}"
        }
    ]
}

然后,你就可以单步运行调试程序了。

当然,launch.json根据你的需要编写即可,例如,假设你有一个项目名称是example,那你可以这样写,

因为这里不打算过多介绍这个launch.json文件,所以只是稍作讲解。

视频如下,

如何用vsCode实现单步调试Rust

本文结束。

相关推荐
该用户已不存在2 天前
Mojo vs Python vs Rust: 2025年搞AI,该学哪个?
后端·python·rust
大卫小东(Sheldon)2 天前
写了一个BBP算法的实现库,欢迎讨论
数学·rust
侃侃_天下2 天前
最终的信号类
开发语言·c++·算法
echoarts2 天前
Rayon Rust中的数据并行库入门教程
开发语言·其他·算法·rust
Aomnitrix2 天前
知识管理新范式——cpolar+Wiki.js打造企业级分布式知识库
开发语言·javascript·分布式
每天回答3个问题2 天前
UE5C++编译遇到MSB3073
开发语言·c++·ue5
伍哥的传说2 天前
Vite Plugin PWA – 零配置构建现代渐进式Web应用
开发语言·前端·javascript·web app·pwa·service worker·workbox
小莞尔2 天前
【51单片机】【protues仿真】 基于51单片机八路抢答器系统
c语言·开发语言·单片机·嵌入式硬件·51单片机
我是菜鸟0713号2 天前
Qt 中 OPC UA 通讯实战
开发语言·qt
JCBP_2 天前
QT(4)
开发语言·汇编·c++·qt·算法