Article directory
1. vscode runs and debugs rust programs
Environment: Use vscode (1) in WSL (ubuntu20.04)
to install rust and cargo in WSL
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) Install rust-analyzer in vscode
(3) Configure vscode compilation: open vscode, terminal -> run task -> configure task -> use template to generate task.json file -> others, tasks.json will be generated in .vscode in the project directory
The reference configuration is as follows:
{
// 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"
]
}
]
}
Compile: Terminal -> Run Task -> wangji rust build ->
- After installing rust-analyzer, in fact, you can see the running task of rust in the terminal->running task, but it feels not easy to use, because the command cannot add custom parameters
- The running configuration is similar, just add a task in tasks.json
(4) Configure vscode debugging: Clicking vscode here
will also generate a launch.json file in .vscode under the project directory.
The available configurations are as follows:
{
// 使用 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"
}
]
}
debugging:
2. Frequently asked questions
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
Observe the vscode output and find that the rustfmt command is not installed, just install it:
wangji@script-wang:~/code/rust/greeting/src$ sudo apt install rustfmt -y
2.cargo command
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. Use rust-gdb to debug rust programs
For specific usage, see: Debugging Rust apps with GDB
4. Cargo build is too slow
Open or create a new /.cargo/config file in the user directory
Add configuration to the file
[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: