太好了!下面为你添加 Rust 开发环境,完整集成 LSP、调试、测试、格式化等专业功能。
🦀 Rust 开发环境配置
1. 创建 Rust 环境文件 (lua/envs/rust.lua)
-- ~/.config/nvim/lua/envs/rust.lua
return {
-- LSP (rust-analyzer)
{
"neovim/nvim-lspconfig",
version = "v2.9.0",
config = function()
local on_attach = require("nvchad.configs.lspconfig").on_attach
local capabilities = require("nvchad.configs.lspconfig").capabilities
require("lspconfig").rust_analyzer.setup({
on_attach = on_attach,
capabilities = capabilities,
settings = {
["rust-analyzer"] = {
cargo = {
allFeatures = true,
loadOutDirsFromCheck = true,
},
procMacro = {
enable = true,
},
checkOnSave = {
command = "check",
},
}
}
})
end,
},
-- 调试 (codelldb + nvim-dap)
{
"mfussenegger/nvim-dap",
dependencies = {
"rcarriga/nvim-dap-ui",
"theHamsta/nvim-dap-virtual-text", -- 调试时显示变量值
},
config = function()
local dap = require("dap")
-- 安装 codelldb 适配器(如果不存在)
local codelldb_path = vim.fn.stdpath("data") .. "/lldb/codelldb"
local lib_codelldb_path = codelldb_path .. "/lib/libcodelldb.so"
if not vim.loop.fs_stat(codelldb_path) then
vim.fn.system({
"git", "clone", "--depth", "1",
"https://github.com/vadimcn/vscode-lldb.git",
codelldb_path
})
end
dap.adapters.codelldb = {
type = "server",
port = "${port}",
executable = {
command = codelldb_path .. "/extension/adapter/codelldb",
args = { "--port", "${port}" },
},
}
dap.configurations.rust = {
{
name = "Launch file",
type = "codelldb",
request = "launch",
program = function()
return vim.fn.input('Path to executable: ', vim.fn.getcwd() .. '/', 'file')
end,
cwd = "${workspaceFolder}",
stopOnEntry = false,
},
{
name = "Attach to process",
type = "codelldb",
request = "attach",
pid = require("dap.utils").pick_process,
cwd = "${workspaceFolder}",
},
}
end,
},
-- 测试运行 (cargo test)
{
"nvim-neotest/neotest",
dependencies = {
"nvim-neotest/neotest-rust",
},
config = function()
require("neotest").setup({
adapters = {
require("neotest-rust")({
cargo = {
args = { "--no-fail-fast" }, -- 继续运行所有测试
},
}),
},
output = {
enabled = true,
expand_on_run = true,
},
quickfix = {
enabled = true,
open = true,
},
})
end,
},
-- 代码质量 (clippy)
{
"mfussenegger/nvim-lint",
config = function()
require("lint").linters_by_ft = {
rust = { "clippy" }
}
vim.api.nvim_create_autocmd("BufWritePost", {
callback = function()
require("lint").try_lint()
end,
})
end,
},
-- 格式化 (rustfmt)
{
"stevearc/conform.nvim",
config = function()
require("conform").setup({
formatters_by_ft = {
rust = { "rustfmt" },
},
format_on_save = {
timeout_ms = 2000, -- Rust 格式化可能较慢
},
})
end,
},
-- Rust 特定工具
{
"simrat39/rust-tools.nvim",
dependencies = "neovim/nvim-lspconfig",
config = function()
require("rust-tools").setup({
server = {
on_attach = require("nvchad.configs.lspconfig").on_attach,
capabilities = require("nvchad.configs.lspconfig").capabilities,
},
tools = {
inlay_hints = {
auto = true,
show_parameter_hints = true,
parameter_hints_prefix = "",
other_hints_prefix = "",
},
},
})
end,
},
-- 语法高亮
{
"nvim-treesitter/nvim-treesitter",
config = function()
require("nvim-treesitter.configs").setup({
ensure_installed = { "rust", "toml" },
highlight = { enable = true },
})
end,
},
-- 增强功能(共享)
{ "lewis6991/gitsigns.nvim", config = true },
{ "ahmedkhalf/project.nvim", config = true },
{ "akinsho/toggleterm.nvim", config = true },
}🔧 Rust 开发依赖安装
1. 安装 Rust 工具链
# 安装 Rustup(如果未安装)
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
# 安装必要组件
rustup component add rust-src rust-analyzer clippy rustfmt2. 验证安装
# 检查版本
rustc --version
cargo --version
rust-analyzer --version # 可能需要单独安装
# 如果 rust-analyzer 不存在,安装它
brew install rust-analyzer # macOS
# 或
cargo install rust-analyzer⌨️ Rust 专用快捷键(已集成到统一系统)
由于你的统一快捷键系统已经包含通用功能,Rust 环境会自动获得:
<leader>gd→ 跳转定义<leader>gR→ 重命名<F5>→ 调试(Rust 支持)<leader>tn→ 运行测试(cargo test)<leader>fd→ 格式化(rustfmt)
Rust 特定功能(通过 rust-tools. Nvim)
gr→ 显示引用gi→ 显示实现ga→ 显示类型注解
🚀 使用 Rust 环境
1. 切换到 Rust 环境
venv rust2. 启动 Neovim
nvim src/main.rs3. 典型工作流
// 编辑代码
fn main() {
println!("Hello, world!");
}
// <leader>fd 格式化代码
// <leader>tn 运行测试
// <F5> 调试程序
// <leader>gs 暂存 Git 变更📦 完整环境列表
现在你拥有 5 个专业开发环境:
| 环境 | 语言/用途 | 核心功能 |
|---|---|---|
python | Python | Pyright, debugpy, pytest, ruff |
typescript | TypeScript | ts_ls, DAP-JS, ESLint, Prettier |
web | HTML/CSS/JS | html/cssls, Emmet, Stylelint |
writer | Markdown/LaTeX | Goyo, 拼写检查, 表格编辑 |
rust | Rust | rust-analyzer, codelldb, cargo, clippy |
💡 Rust 环境验证清单
-
rustc --version正常输出 -
cargo new test-project创建项目 -
venv rust切换环境 -
nvim src/main.rs启动 Neovim -
<leader>gd跳转到println!定义 -
<leader>tn运行测试(如果有测试文件)
🎯 高级 Rust 功能
1. Cargo 集成
在终端中(Ctrl+\):
cargo build
cargo run
cargo test
cargo clippy2. 调试配置
- Launch: 调试当前可执行文件
- Attach: 附加到正在运行的进程
3. Inlay Hints
自动显示类型注解和参数名,类似 JetBrains 的智能提示。
你现在拥有了一个 完整的 Rust 开发环境,具备专业 IDE 的所有核心功能!需要我帮你创建其他语言环境(如 Go、C++)吗?