惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
云风的 BLOG
云风的 BLOG
aimingoo的专栏
aimingoo的专栏
Vercel News
Vercel News
T
The Blog of Author Tim Ferriss
F
Full Disclosure
A
About on SuperTechFans
C
Check Point Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
量子位
Know Your Adversary
Know Your Adversary
K
Kaspersky official blog
L
LINUX DO - 热门话题
Recorded Future
Recorded Future
C
Cisco Blogs
M
MIT News - Artificial intelligence
T
Tenable Blog
G
GRAHAM CLULEY
月光博客
月光博客
Recent Announcements
Recent Announcements
V
Visual Studio Blog
IT之家
IT之家
T
The Exploit Database - CXSecurity.com
The GitHub Blog
The GitHub Blog
T
Threat Research - Cisco Blogs
D
DataBreaches.Net
P
Privacy International News Feed
P
Proofpoint News Feed
I
Intezer
博客园 - 叶小钗
C
CXSECURITY Database RSS Feed - CXSecurity.com
The Hacker News
The Hacker News
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
博客园 - Franky
SecWiki News
SecWiki News
宝玉的分享
宝玉的分享
P
Palo Alto Networks Blog
Last Week in AI
Last Week in AI
小众软件
小众软件
Hacker News - Newest:
Hacker News - Newest: "LLM"
O
OpenAI News
N
News and Events Feed by Topic
Microsoft Security Blog
Microsoft Security Blog
Security Archives - TechRepublic
Security Archives - TechRepublic
N
News and Events Feed by Topic
The Cloudflare Blog
Spread Privacy
Spread Privacy
酷 壳 – CoolShell
酷 壳 – CoolShell
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
B
Blog RSS Feed

逸思杂陈

家用网络 vlan 单线复用 Linux 平台 intel UHD 6xx 核显 openvino 探索 UI 区域检测的 vibe coding 复盘 mitmproxy 使用 esim 使用相关 阻止 bilibili 网页自动关注 Hexo 版本更新与技术债务 配置 Linux 作为主力操作系统 在 Linux 虚拟机中使用 PyAutoGUI 做自动化 语言的力量 联想笔记本 BIOS 跳过检测强制降级 redroid “设备未获得play保护机制认证” 问题 在 iOS 上访问安卓应用 跨域的那些事 HomeBrew 与无 root 权限 Linux 环境包管理 给 macOS 词典增加生词本功能 关闭子进程打开的文件描述符 容器内进程优雅退出 Python 循环变量泄露与延迟绑定 bash 语法备忘 MySQL 自定义数据库路径
在 VSCode 中用 Rust 刷LeetCode
Jay.Run · 2025-03-09 · via 逸思杂陈

本文介绍在 VSCode 中配置和使用插件来高效地解决 LeetCode 问题,并使用 Rust 语言编写和测试代码。

  • LeetCode.vscode-leetcode
  • pucelle.run-on-save
  • rust-lang.rust-analyzer

项目结构

cargo new vscode-leetcode-rust

1
2
3
4
5
6
7
8
9
10

.
├── Cargo.lock
├── Cargo.toml
└── src
├── lib.rs
├── main.rs
└── solutions
├── 1_two_sum.rs
...

vscode 全局设置

1
2
3
4
5
6
7
8
"leetcode.useEndpointTranslation": false,  
"leetcode.workspaceFolder": "/Users/<your_name>/projects/vscode-leetcode-cn-rust",
"leetcode.filePath": {
"default": {
"folder": "src/solutions",
"filename": "${id}_${snake_case_name}.${ext}"
}
},

用 automod 宏添加新回答到模块

cargo add automod

1
2
3
4
5
6
7


const CURRENT: &str = "sdfsdfsd.rs";

pub mod solutions {
automod::dir!("src/solutions");
}

触发 rust-analyzer

用 run-on-save 插件,保存回答时更新 lib.rs,触发 rust-analyzer 重新分析项目,开启新回答的代码补全。

vscode 项目配置

1
2
3
4
5
6
7
8
"runOnSave.commands": [
{

"command": "sh onsave.sh ${fileBasename}",
"runIn": "backend",
"finishStatusMessage": "touched ${workspaceFolderBasename}"
},
]

onsave.sh脚本,macOS使用 gnused,linux 使用默认 sed 就好。
通过切换模块是否为pub来触发 rust-analyzer 识别新回答

1
2
3
4
5
6
7
8
9
10
11
12
#!/bin/bash

FILE=$1
if [[ `grep "$FILE" src/lib.rs | wc -l` -eq 0 ]]; then
gsed -i -E "/CURRENT/c\const CURRENT: &str = \"$FILE\";" src/lib.rs
if [[ `grep '::dir!(pub' src/lib.rs | wc -l` -eq 1 ]]; then
gsed -i "s|::dir!(pub |::dir!(|" src/lib.rs
else
gsed -i "s|::dir!(|::dir!(pub |" src/lib.rs
fi
fi

编写本地测试用例

把测试代码写在 "// @lc code=end" 后面,需要定义 Solution 结构体,可能还需要定义参数的结构体。

1
2
3
4
5
6
7
8
// @lc code=end
struct Solution;


fn test_a() {
// let res = Solution::is_valid(String::from("()[]{}"));
// println!("RESUTL\t{:?}", res);
}

-w340