vscode中调试rust程序

2023-05-16

文章目录

  • 一、vscode运行和调式rust程序
  • 二、常见问题
    • 1.rust: Request textDocument/formatting failed.
    • 2.cargo命令
    • 3.使用rust-gdb调试rust程序
    • 4.cargo build太慢

一、vscode运行和调式rust程序

环境:在WSL(ubuntu20.04)中使用vscode
(1)在WSL 中安装rust,cargo

wangji@script-wang:~/code/rust/greeting/src$ sudo apt install rustc -y
wangji@script-wang:~/code/rust/greeting/src$ sudo apt install rustfmt -y
wangji@script-wang:~/code/rust/greeting/src$ sudo apt-get -y install cargo

(2)在vscode中安装rust-analyzer

(3)配置vscode编译:打开vscode,终端->运行任务->配置任务->使用模板生成task.json文件->others,会在工程目录的.vscode中生成tasks.json

参考配置如下:

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "label": "wangji rust build debug",
            "type": "shell",
            "command": "cargo",
            "args": [
                "build"
            ]
        },
        {
            "label": "wangji rust build release",
            "type": "shell",
            "command": "cargo",
            "args": [
                "build --release"
            ]
        }
    ]
}

编译:终端->运行任务->wangji rust build->

  • 安装完rust-analyzer,其实在终端->运行任务可以看到rust的运行任务,但是感觉不好用,因为命令不能添加自定义参数
  • 运行的配置类似,增加个task在tasks.json即可

(4)配置vscode调试:点击vscode这里
在这里插入图片描述
也会在工程目录下的.vscode中生成一个launch.json文件
可用的配置如下:

{
    // 使用 IntelliSense 了解相关属性。 
    // 悬停以查看现有属性的描述。
    // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Debug",
            "type": "gdb",
            "request": "launch",
            "target": "${workspaceFolder}/target/debug/${workspaceFolderBasename}",
            "cwd": "${workspaceRoot}",
            "valuesFormatting": "parseText"
        },
        {
            "name": "Release",
            "type": "gdb",
            "request": "launch",
            "target": "${workspaceFolder}/target/release/${workspaceFolderBasename}",
            "cwd": "${workspaceRoot}",
            "valuesFormatting": "parseText"
        }
    ]
}

调试:
在这里插入图片描述

二、常见问题

1.rust: Request textDocument/formatting failed.

[Error - 10:02:32 AM] Request textDocument/formatting failed.
  Message: Failed to spawn cd "/home/wangji/code/rust/greeting/src" && "rustfmt" "--edition" "2021"
  Code: -32603 

观察vscode输出发现没有安装rustfmt命令,安装即可:

wangji@script-wang:~/code/rust/greeting/src$ sudo apt install rustfmt -y

2.cargo命令

cargo clippy: 类似eslint,lint工具检查代码可以优化的地方
cargo fmt: 类似go fmt,代码格式化
cargo tree: 查看第三方库的版本和依赖关系
cargo bench: 运行benchmark(基准测试,性能测试)
cargo udeps(第三方): 检查项目中未使用的依赖
另外 cargo build/run --release 使用 release 编译会比默认的 debug 编译性能提升 10 倍以上,但是 release 缺点是编译速度较慢,而且不会显示 panic backtrace 的具体行号

3.使用rust-gdb调试rust程序

具体用法见:Debugging Rust apps with GDB

4.cargo build太慢

打开或新建用户目录下/.cargo/config文件
在文件中增加配置

[source.crates-io]
registry = "https://github.com/rust-lang/crates.io-index"

replace-with = 'tuna'
[source.tuna]
registry = "https://mirrors.tuna.tsinghua.edu.cn/git/crates.io-index.git"

ref:

  • vscode使用tasks.json执行c++多文件编译
  • Cargo 教程
  • Debugging Rust apps with GDB
  • Rust解决cargo build太慢
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

vscode中调试rust程序 的相关文章

随机推荐

  • PELCO-D协议 要点整理

    消息格式 Byte 1Byte 2Byte 3Byte 4Byte 5Byte 6Byte 7Sync ByteAddressCommand 1Command 2Data 1Data 2Checksum The synchronizatio
  • GTEST/GMOCK介绍与实战:Gtest Sample9

    文章目录 1 简介2 用法 1 简介 示例 9显示了使用侦听器API修改谷歌Test的控制台输出和使用其反射API来检查测试结果 2 用法 span class token comment This sample shows how to
  • Gtest输出单元测试报告和输出覆盖率报告

    文章目录 1 要求2 生成gtest测试报告3 生成gtest覆盖率报告 1 要求 编译工具 xff1a 选择Cmake xff0c 单元测试使用Gtest 2 生成gtest测试报告 gtest本身仅能输出xml或者json格式的测试报告
  • GTEST/GMOCK介绍与实战:Gtest Sample10

    文章目录 1 简介2 用法 1 简介 示例 10展示了如何使用侦听器API来实现基本内存泄漏检查 2 用法 span class token comment This sample shows how to use Google Test
  • Bitbake与Yocto

    文章目录 一 Bitbake二 Yocto 一 Bitbake xff08 1 xff09 使用教程可以参考 xff1a BitBake 实用指南 xff0c 大部分步骤跟着操作即可了解bitbake的工作流程 xff1b 他主要参考和翻译
  • 随机漫步

    span class token keyword import span numpy span class token keyword as span np span class token keyword import span rand
  • UTC时间和PTP精确时间协议

    文章目录 一 GMT二 UTC三 GMT vs UTC四 C 43 43 获得当前的UTC时间 一 GMT GMT xff08 Greenwich Mean Time xff09 xff0c 格林威治平时 xff08 也称格林威治时间 xf
  • AutoSar系列之:AutoSar发展

    文章目录 一 Autosar成员二 Autosar历史发展三 使用Autosar前的状态1 原始状态2 进阶状态 四 使用Autosar后的状态1 软硬件隔离2 Autosar优势 一 Autosar成员 二 Autosar历史发展 三 使
  • AutoSar系列之:AutoSar概述

    文章目录 一 Autosar是什么二 架构 一 Autosar是什么 RTE xff1a 用与传递应用层软件和基础软件从之间的信号的 xff1b 隔离应用软件层和基础软件层 xff1b 其中一个层修改了 xff0c 不会影响另外一个层 xf
  • Autosar系列之Appl概述

    文章目录 一 Appl的组成1 SWC通信2 SWC分配 一 Appl的组成 SWC xff1a 应用软件组件 Autosar接口 xff1a SWC之间连接的端口 Runnable xff1a 可运行实体 xff0c SWC里面的一些函数
  • Autosar系列之SWC类型

    文章目录 一 原子级SWC二 集合级SWC三 特殊的SWC 一 原子级SWC 含义 xff1a 不可拆解的SWC 二 集合级SWC eg xff1a 将相似的功能放在一起 三 特殊的SWC IoHwAb xff0c Cdd 在原有的Auto
  • 汽车操作系统

    文章目录 一 汽车控制器类型二 Hypervisor三 QNX Linux Andorid四 Automotive Grade Linux 系统 xff08 AGL xff09 1 介绍2 IVI市场现状3 系统构建 xff08 1 xff
  • Autosar系列之Ports类型

    文章目录 一 接口二 接口类型三 S R接口四 C S 接口 一 接口 接口是连接2个SWC通信的 二 接口类型 三 S R接口 发送 接受数据传输接口 一般通过全局变量才传递 四 C S 接口 客户 服务接口 xff1b 通过函数Runn
  • Autosar系列之Runnable可运行实体

    文章目录 一 RUnnable Entity 一 RUnnable Entity 可运行实体 xff0c 其实就是 C文件内的函数而已 一个SWC可以包含多个Runnable Entity xff0c 就是一个 C文件中可以包含多个函数 x
  • Autosar系列之RTE

    文章目录 一 RTE二 RTE功能 一 RTE RTE Run TIme Environment 是Autosar体系结构的核心 RTE是Autosar软件架构中 xff0c 介于应用层和基础软件层之间 xff0c 是Autosar虚拟功能
  • Autosar系列之Autosar应用层整体入门

    文章目录 一 整个功能示意图二 软件组件SWC分类三 SWC组件 xff1a ports1 发送 接收端口Sender Receiver2 客户端 服务端端口Client Server 四 可运行实体Runnables五 BSW1 微控制器
  • ubuntu下mysql数据库的设置

    gt su root gt mysql span class token operator span u root span class token operator span p gt show databases span class
  • Python装饰器Decorators

    文章目录 一 功能二 64 语法糖三 args kwargs四 带参数的装饰器五 类装饰器六 装饰器顺序 一 功能 装饰器本质上是一个 Python 函数或类 xff0c 它可以让其他函数或类在不需要做任何代码修改的前提下增加额外功能 xf
  • Autosar系列之Developer工具

    文章目录 一 什么是DaVinci Developer xff1f 二 DaVinci Developer Workspace三 Software Conponent xff08 SWC xff09 Design 一 什么是DaVinci
  • vscode中调试rust程序

    文章目录 一 vscode运行和调式rust程序二 常见问题1 rust Request textDocument formatting failed 2 cargo命令3 使用rust gdb调试rust程序4 cargo build太慢