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

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

学习资料网站

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

  • Rust程序设计语言
  • Rust语言圣经(Rust Course)
  • Rust 官方文档中文教程
  • Rust语言中文社区
  • 跟Google学Rust
  • 官网 Learn Rust
  • The Rust Programming Language
  • The Rust Programming Language (interactive learning experience)
  • rustlings
  • 跟例子学Rust
  • Rust标准库
  • Rust 版本指南
  • Cargo详解
  • Rust编译器介绍
  • Rust编译器报错说明手册
  • Rust 完全手册
  • Rust 'nomicon - unsafe黑暗魔法卷轴

给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,那你可以这样写,

RUST入门:如何用vscode调试rust程序_第1张图片

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

视频如下,

如何用vsCode实现单步调试Rust

本文结束。

 

你可能感兴趣的:(rust,vscode,开发语言)