本文介绍在 VSCode 中配置和使用插件来高效地解决 LeetCode 问题,并使用 Rust 语言编写和测试代码。
vscode 插件
- LeetCode.vscode-leetcode
- pucelle.run-on-save
- rust-lang.rust-analyzer
项目结构
cargo new vscode-leetcode-rust
1 | // tree -I target --dirsfirst |
vscode 全局设置
1 | "leetcode.useEndpointTranslation": false, // for english filename |
用 automod 宏添加新回答到模块
cargo add automod
1 | // src/lib.rs |
触发 rust-analyzer
用 run-on-save 插件,保存回答时更新 lib.rs,触发 rust-analyzer 重新分析项目,开启新回答的代码补全。
vscode 项目配置1
2
3
4
5
6
7
8"runOnSave.commands": [
{
// use gnu sed update lib.rs when add solutions (macOS)
"command": "sh onsave.sh ${fileBasename}",
"runIn": "backend",
"finishStatusMessage": "touched ${workspaceFolderBasename}"
},
]
onsave.sh
脚本,macOS使用 gnused,linux 使用默认 sed 就好。
通过切换模块是否为pub来触发 rust-analyzer 识别新回答
1 |
|
编写本地测试用例
把测试代码写在 "// @lc code=end"
后面,需要定义 Solution 结构体,可能还需要定义参数的结构体。
1 | // end code= |