VSCode调试Rust必须安装CodeLLDB插件,因VS Code原生不支持Rust调试,rust-analyzer仅负责代码分析;断点不命中主因是debug信息缺失或launch.json中program路径写死,须用变量动态路径并确保cargo build已执行。

VSCode 调试 Rust 必须装 CodeLLDB,不能靠默认调试器;断点不命中几乎全是 debug 信息缺失或 launch.json 路径写死导致的。
必须装 CodeLLDB 插件,不是 rust-analyzer 也不是 VS Code 自带调试器
VS Code 原生不支持 Rust 调试,rust-analyzer 只负责代码分析,不提供调试能力。Windows/macOS/Linux 都得装 CodeLLDB(作者 Vadim Chugunov),别装错成旧版 vscode-lldb 或 lldb 命令行工具本身。
- 在 VS Code 扩展市场搜
CodeLLDB,认准图标是蓝色龙虾+“by Vadim Chugunov” - macOS 用户:系统自带
lldb,不用额外装;Linux(如 Ubuntu)需sudo apt install lldb;Windows 必须装 Visual Studio 2022(带 “C++ build tools” 工作负载)或单独装 LLVM - 装完重启 VS Code,否则调试按钮灰色不可点
launch.json 必须用变量动态路径,不能硬编码 ./target/debug/xxx
写死路径是断点失效最常见原因:Rust 默认 cargo build 输出到 ./target/debug/<name></name>,但项目名可能含下划线、大小写不一致,或你用了 cargo build --bin myapp 指定二进制名,硬编码就直接报 cannot find program。
- 在项目根目录(含
Cargo.toml)按Ctrl+Shift+P→ 输入Debug: Open launch.json→ 选CodeLLDB模板 - 把
"program"改成:"${workspaceFolder}/target/debug/${fileBasenameNoExtension}"(适用于单main.rs文件)或更稳妥的"${workspaceFolder}/target/debug/${workspaceFolderBasename}" - 确保
"cargo"字段没被手动删掉——CodeLLDB 模板里默认有"cargo": { "args": ["build"] },它会自动触发 debug 构建
必须生成带 debug info 的二进制,release 模式默认不支持断点
即使 launch.json 正确,如果实际构建的是 release 版,断点永远不命中。Rust 默认 cargo build --release 会 strip debug info,且 profile.release.debug = false 是默认行为。
- 检查
Cargo.toml是否误加了[profile.release]段并设了debug = false—— 删掉整段或改成debug = true - 手动验证:终端执行
cargo build(无--release),然后看./target/debug/下是否有对应可执行文件,且file ./target/debug/myapp输出含with debug_info - Windows 上若报
cannot find native Windows debugger,说明没装 C++ 构建工具,不是插件问题
rust-src 组件没装会导致标准库跳转失败,但不影响断点本身
调试时能停在 main,却点不进 Vec::new() 或 println! 宏展开,大概率是缺 rust-src。这不是调试失败,而是符号解析不全。
- 运行
rustup component add rust-src补上(注意:不是cargo install rust-src) - 装完后在 VS Code 里按
Ctrl+Shift+P→Rust Analyzer: Reload Workspace,等状态栏显示Ready - 如果仍跳不进标准库,检查
rust-analyzer.rustcSource设置是否为discover(默认值),别设成from_sysroot后又没指定 sysroot 路径
真正卡住调试的,从来不是插件装没装,而是 cargo build 没跑过、launch.json 路径和实际输出对不上、或者 Windows 下连 C++ 构建工具都没装。先在终端敲一遍 cargo build && ./target/debug/myapp 确认能跑,再进 VS Code 调试,顺序不能反。


















