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文件,里面的代码是

fn main() {
    println!("Hello, world!");
}

在这一句打上断点。

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

Create a launch.json file

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

{
    // 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

本文结束。

 

  • 9
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值