我们今天来配置下vscode+rust。
vscode开发rust很方便。但配置有点坑,我们都认为vscode很简单,很完善。
但这里很多同学也出现不少问题。
我们在这里简单记录下win7下配置的过程,跟着我一步步来,应该就可打造你的屠龙宝刀。
首先,我们安装插件:
Rust Extension Pack
Rust Test Explorer
然后打开上一篇文章的工程:`hello-rust`,见:# rustlang语言零基础快速入门(1)
打开command palette (Ctrl-Shift-P):输入:build,然后选择:Tasks: Configure Default Build Task,再选择:Rust: cargo build
![](https://img2018.cnblogs.com/blog/544318/201911/544318-20191119111544669-1539794949.png)
![](https://img2018.cnblogs.com/blog/544318/201911/544318-20191119111752805-826086223.png)
vscode会自动生成一个json文件:
**// See https://go.microsoft.com/fwlink/?LinkId=733558 **
** // for the documentation about the tasks.json format**
** "version": "2.0.0",**
** "tasks": \[**
** {**
** "label": "cargo run",**
** "type": "shell",**
** "command": "cargo",**
** "args": \[**
** "run"**
** \],**
** "group": {**
** "kind": "build",**
** "isDefault": true**
** }**
** }**
** 这里,我们直接按“CTRL+SHIFT+P”,选择:“Task:Run Build Task”,或者直接按快捷键“CTRL+F9”**
![](https://img2018.cnblogs.com/blog/544318/201911/544318-20191120103110135-538352550.png)
VSCODE会自动BUILD,并在终端窗口显示打印结果:
\-------------------------------------------------------
\> Executing task: cargo run <
Compiling hello-rust v0.1.0 (E:\\code\\rustProject\\hello-rust)
Finished dev \[unoptimized + debuginfo\] target(s) in 2.11s
Running `target\\debug\\hello-rust.exe`
\----------------------------
| Hello fellow Rustaceans! |
\----------------------------
\\
\\
\_~^~^~\_
\\) / o o \\ (/
'\_ - \_'
/ '-----' \\
Terminal will be reused by tasks, press any key to close it.
\--------------------------------------------------------------
下面配置测试task:
先在main函数下面增加测试代码:
~~~
#[test]
fn should_fail() {
unimplemented!();
}
保存后,按快捷键:“CTRL+SHIFT+P”,输入:Task,选择“Tasks: Configure Default Test Task”,然后选择:“Rust: cargo test”
~~~
![](https://img2018.cnblogs.com/blog/544318/201911/544318-20191120103954112-1485635816.png)
![](https://img2018.cnblogs.com/blog/544318/201911/544318-20191120104027408-1155904162.png)
~~~
vscode自动生成:
~~~
{
"type": "cargo",
"subcommand": "test",
"problemMatcher": \[
"$rustc"
\],
"group": {
"kind": "test",
"isDefault": true
}
}
~~~
保存后,按按快捷键:“CTRL+SHIFT+P”,输入:Task:Run test Task,回车。
vscode自动运行测试用例,并打印结果:
---------------------------------------
> Executing task: cargo test <
Compiling hello-rust v0.1.0 (E:\code\rustProject\hello-rust)
Finished dev [unoptimized + debuginfo] target(s) in 1.77s
Running target\debug\deps\hello_rust-bfa762df5afd173e.exe
running 1 test
test should_fail ... FAILED
failures:
---- should_fail stdout ----
thread 'should_fail' panicked at 'not yet implemented', src\main.rs:14:5
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace.
failures:
should_fail
test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out
error: test failed, to rerun pass '--bin hello-rust'
The terminal process terminated with exit code: 1
Terminal will be reused by tasks, press any key to close it.
-----------------------------------------------------------
下面继续配置DEBUG环境。
这里参照这个文章:https://www.forrestthewoods.com/blog/how-to-debug-rust-with-visual-studio-code/
简单来说,你按如下几步来配置就可以:
1.安装:
C/C++ extension.
Native Debug extension
2.在debug面板,新建一个新的配置,如下:
~~~
![](https://img2018.cnblogs.com/blog/544318/201911/544318-20191120111411506-813620503.png)
然后选择:“C++ (Windows)” environment
会自动生成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": \[
{
"name": "(Windows) Launch",
"type": "cppvsdbg",
"request": "launch",
"program": "${workspaceFolder}/target/debug/hello-rust.exe",
"args": \[\],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": \[\],
"externalConsole": false
}
\]
}
~~~
其中program的值改为你自己的exe的路径,比如我的就是:
${workspaceFolder}/target/debug/hello-rust.exe
~~~
~~~
这时,你直接按F5,你就进入debug状态,你现在可以设置断点了。
~~~
~~~
如果,你顺利走到这一步,恭喜你,你已经基本配置好rust的开发环境。
~~~
- rustlang语言零基础快速入门(1)开篇
- rustlang语言零基础快速入门(2)VSCODE配置
- rustlang语言零基础快速入门(3)所有权Ownership
- rustlang语言零基础快速入门(4)
- rustlang语言零基础快速入门(5)
- rustlang语言零基础快速入门(6)变量绑定
- rustlang语言零基础快速入门(7)函数Functions与闭包Closure
- rustlang语言零基础快速入门(8)Operators操作符
- rustlang语言零基础快速入门(9)Control Flows流程控制
- rustlang语言零基础快速入门(10)Vectors容器
- rustlang语言零基础快速入门(11)Structs结构体
- rustlang语言零基础快速入门(12)Enums枚举
- rustlang语言零基础快速入门(13)Generics泛型
- rustlang语言零基础快速入门(14)Impls & Traits实现与特征
- rustlang语言零基础快速入门(15)Unit Testing单元测试
- rustlang语言零基础快速入门(16)代码组织与模块化
- rustlang语言零基础快速入门(17)装箱crates
- rustlang语言零基础快速入门(18)use关键词
- rustlang语言零基础快速入门(19)多线程
- rustlang语言零基础快速入门(20)错误处理
- rustlang语言零基础快速入门(21)智能指针
- rustlang语言零基础快速入门(22)宏Macro
- rustlang语言零基础快速入门(23)实战1:猜数字游戏
- rustlang语言零基础快速入门(24)实战2:命令行工具minigrep(1)
- rustlang语言零基础快速入门(25)实战2:命令行工具minigrep(2)
- rustlang语言零基础快速入门(26)实战3:Http服务器
- rustlang语言零基础快速入门(26)实战3:Http服务器(多线程版本)
- rustlang语言零基础快速入门(27)实战4:从零实现BTC区块链
- rustlang语言零基础快速入门(28)实战5:实现BTC价格转换工具
- rustlang语言零基础快速入门(29)实战6:BDD工具cucumber_rust