当前位置:   article > 正文

Rust从入门到暴走(3)--- Rust开发环境搭建Visual Studio Code

rust开发环境

通过上篇文章相信大家已经学会了如何通过命令行的形式编译Rust代码文件并且运行,今天我们来学习如何搭建好自己的的开发环境,这里选用的是Visual Studio Code

下载Visual Studio Code

Visual Studio Code:Download Visual Studio Code - Mac, Linux, Windows

启动Visual Studio Code

下载完 Visual Studio Code 安装包之后启动安装向导安装。 安装完 Visual Studio Code (下文简称 VSCode)之后运行 VSCode。

安装插件

rust analyzer & native debug (安装好,重启VSCode就可以)

创建项目

Terminal--->New Terminal

创建Rust的项目demo

  1. luca@LucadeMacBook-Air rust-workspace % cargo new demo
  2. Created binary (application) `demo` package
  3. luca@LucadeMacBook-Air rust-workspace % ls -a
  4. . .. demo
  5. luca@LucadeMacBook-Air rust-workspace %

Cargo构建和运行

Cargo 是Rust 编译器Rust的构建工具和前端,以及软件包和依赖项管理器。 它允许Rust 项目声明具有特定版本要求的依赖关系,解析完整依赖项图、下载软件包和构建,以及测试整个项目。

  1. luca@LucadeMacBook-Air rust-workspace % cd demo
  2. luca@LucadeMacBook-Air demo %
  3. luca@LucadeMacBook-Air demo % cargo build
  4. Compiling demo v0.1.0 (/Users/luca/dev/rust-workspace/demo)
  5. Finished dev [unoptimized + debuginfo] target(s) in 5.45s
  6. luca@LucadeMacBook-Air demo %
  7. luca@LucadeMacBook-Air demo % cargo run
  8. Finished dev [unoptimized + debuginfo] target(s) in 0.00s
  9. Running `target/debug/demo`
  10. Hello, world!

VSCode界面运行

通过VSCode打开项目demo

项目目录结构,通过VSCode运行代码文件

VSCode集成Debug

在demo目录里面新建.vscode文件夹,加入tasks.json和launch.json

task.json

  1. {
  2. "version": "2.0.0",
  3. "tasks": [
  4. {
  5. "label": "build",
  6. "type": "shell",
  7. "command": "cargo",
  8. "args": [
  9. "build"
  10. ]
  11. }
  12. ]
  13. }

launch.json

对于 Mac OS

  1. {
  2. "version": "0.2.0",
  3. "configurations": [
  4. {
  5. "name": "lldb launch",
  6. "type": "cppdbg",
  7. "preLaunchTask": "build",
  8. "request": "launch",
  9. "program": "${workspaceFolder}/target/debug/${workspaceFolderBasename}",
  10. "args": [],
  11. "stopAtEntry": false,
  12. "cwd": "${workspaceFolder}",
  13. "environment": [],
  14. "externalConsole": false,
  15. "MIMode": "lldb"
  16. }
  17. ]
  18. }

对于 Linux

  1. {
  2. "version": "0.2.0",
  3. "configurations": [
  4. {
  5. "name": "Debug",
  6. "type": "gdb",
  7. "preLaunchTask""build",
  8. "request": "launch",
  9. "target": "${workspaceFolder}/target/debug/${workspaceFolderBasename}",
  10.             "cwd""${workspaceFolder}"
  11. }
  12. ]
  13. }

对于 Windows

  1. {
  2.     "version": "0.2.0",
  3.     "configurations": [
  4.         {
  5.             "name": "Windows launch",
  6.             "preLaunchTask": "build",
  7.             "type": "cppvsdbg",
  8.             "request": "launch",
  9.             "program": "${workspaceFolder}/target/debug/${workspaceFolderBasename}.exe",
  10.             "args": [],
  11.             "stopAtEntry": false,
  12.             "cwd": "${workspaceFolder}",
  13.             "environment": [],
  14.             "externalConsole": false
  15.         },
  16. {
  17.             "name": "gdb launch",
  18.             "type": "cppdbg",
  19.             "request": "launch",
  20.             "program": "${workspaceFolder}/target/debug/${workspaceFolderBasename}.exe",
  21.             "args": [],
  22.             "stopAtEntry": false,
  23.             "cwd": "${workspaceFolder}",
  24.             "environment": [],
  25.             "externalConsole": false,
  26.             "MIMode": "gdb",
  27.             "miDebuggerPath": "这里填GDB所在的目录",
  28.             "setupCommands": [
  29.                 {
  30.                     "description": "为 gdb 启用整齐打印",
  31.                     "text": "-enable-pretty-printing",
  32.                     "ignoreFailures": true
  33.                 }
  34.             ]
  35.         }
  36.     ]
  37. }

加上断点,点击debug

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/533341
推荐阅读
相关标签
  

闽ICP备14008679号