赞
踩
通过上篇文章相信大家已经学会了如何通过命令行的形式编译Rust代码文件并且运行,今天我们来学习如何搭建好自己的的开发环境,这里选用的是Visual Studio Code
Visual Studio Code:Download Visual Studio Code - Mac, Linux, Windows
下载完 Visual Studio Code 安装包之后启动安装向导安装。 安装完 Visual Studio Code (下文简称 VSCode)之后运行 VSCode。
rust analyzer & native debug (安装好,重启VSCode就可以)
Terminal--->New Terminal
创建Rust的项目demo
- luca@LucadeMacBook-Air rust-workspace % cargo new demo
- Created binary (application) `demo` package
- luca@LucadeMacBook-Air rust-workspace % ls -a
- . .. demo
- luca@LucadeMacBook-Air rust-workspace %
Cargo 是Rust 编译器Rust的构建工具和前端,以及软件包和依赖项管理器。 它允许Rust 项目声明具有特定版本要求的依赖关系,解析完整依赖项图、下载软件包和构建,以及测试整个项目。
- luca@LucadeMacBook-Air rust-workspace % cd demo
- luca@LucadeMacBook-Air demo %
- luca@LucadeMacBook-Air demo % cargo build
- Compiling demo v0.1.0 (/Users/luca/dev/rust-workspace/demo)
- Finished dev [unoptimized + debuginfo] target(s) in 5.45s
- luca@LucadeMacBook-Air demo %
- luca@LucadeMacBook-Air demo % cargo run
- Finished dev [unoptimized + debuginfo] target(s) in 0.00s
- Running `target/debug/demo`
- Hello, world!
通过VSCode打开项目demo
项目目录结构,通过VSCode运行代码文件
在demo目录里面新建.vscode文件夹,加入tasks.json和launch.json
task.json
- {
- "version": "2.0.0",
- "tasks": [
- {
- "label": "build",
- "type": "shell",
- "command": "cargo",
- "args": [
- "build"
- ]
- }
- ]
- }
launch.json
对于 Mac OS
- {
- "version": "0.2.0",
- "configurations": [
- {
- "name": "lldb launch",
- "type": "cppdbg",
- "preLaunchTask": "build",
- "request": "launch",
- "program": "${workspaceFolder}/target/debug/${workspaceFolderBasename}",
- "args": [],
- "stopAtEntry": false,
- "cwd": "${workspaceFolder}",
- "environment": [],
- "externalConsole": false,
- "MIMode": "lldb"
- }
- ]
- }
对于 Linux
-
- {
- "version": "0.2.0",
- "configurations": [
- {
- "name": "Debug",
- "type": "gdb",
- "preLaunchTask": "build",
- "request": "launch",
- "target": "${workspaceFolder}/target/debug/${workspaceFolderBasename}",
- "cwd": "${workspaceFolder}"
- }
- ]
- }
对于 Windows
- {
- "version": "0.2.0",
- "configurations": [
- {
- "name": "Windows launch",
- "preLaunchTask": "build",
- "type": "cppvsdbg",
- "request": "launch",
- "program": "${workspaceFolder}/target/debug/${workspaceFolderBasename}.exe",
- "args": [],
- "stopAtEntry": false,
- "cwd": "${workspaceFolder}",
- "environment": [],
- "externalConsole": false
- },
- {
- "name": "gdb launch",
- "type": "cppdbg",
- "request": "launch",
- "program": "${workspaceFolder}/target/debug/${workspaceFolderBasename}.exe",
- "args": [],
- "stopAtEntry": false,
- "cwd": "${workspaceFolder}",
- "environment": [],
- "externalConsole": false,
- "MIMode": "gdb",
- "miDebuggerPath": "这里填GDB所在的目录",
- "setupCommands": [
- {
- "description": "为 gdb 启用整齐打印",
- "text": "-enable-pretty-printing",
- "ignoreFailures": true
- }
- ]
- }
- ]
- }
加上断点,点击debug
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。