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

本文结束。

相关推荐
Beyond_System|系统之外42 分钟前
【学编程】Python基础编程题100道(21-60)
开发语言·python·算法
景熙55231 小时前
15.Java 8 Stream 流入门到实战
java·开发语言·数据结构
程序员允诺1 小时前
内网/无网环境必备:VS Code Remote-SSH 离线包下载与手动部署教程
vscode
Bruce_Liuxiaowei4 小时前
Python 实例赋值遮蔽类属性:一个从不报错的静默陷阱
开发语言·python·语法糖
梦醒沉醉4 小时前
std1.97.1——cmp模块细览
rust
aramae5 小时前
MySQL内置函数(7)
开发语言·笔记·后端·mysql·其他
PC2005-cloud7 小时前
Rust学习笔记:错误处理——panic!、Result、Option与_运算符
rust
码行山野赴时序归途8 小时前
链表家族收官篇:循环链表
c语言·开发语言·数据结构·链表
景熙55238 小时前
单体项目编写思路和落地形式
java·开发语言
darin_ฅ( ̳• ◡ • ̳)ฅ18 小时前
【开发工具】2026版-VScode&IDEA&Cursor插件安装及插件配置迁移
vscode·idea