当前位置: 首页 > news >正文

在LazyVim中配置Rust开发环境

要在LazyVim中配置Rust开发环境,包括代码补全、格式化、调试等功能,可以按照以下步骤进行配置:

1. 确保基础环境

首先确保你已经安装了:

  • Rust工具链 (rustup, rustc, cargo)
  • LazyVim已正确安装
# 安装Rust工具链
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh

2. 配置Rust LSP服务器 (rust-analyzer)

LazyVim默认使用rust-analyzer作为Rust的LSP服务器:

-- 在 ~/.config/nvim/lua/plugins/lsp.lua 中添加或修改
return {{"neovim/nvim-lspconfig",opts = {servers = {-- 添加rust_analyzer配置rust_analyzer = {settings = {["rust-analyzer"] = {cargo = {allFeatures = true,loadOutDirsFromCheck = true,runBuildScripts = true,},checkOnSave = {command = "clippy",extraArgs = { "--no-deps" },},procMacro = {enable = true,},},},},},-- 设置rust_analyzer自动附加到rust文件setup = {rust_analyzer = function(_, opts)require("lspconfig").rust_analyzer.setup(opts)end,},},},
}

3. 安装Rust专用插件

-- 在 ~/.config/nvim/lua/plugins/extra.lua 中添加
return {-- Rust增强插件{"simrat39/rust-tools.nvim",ft = "rust",dependencies = "neovim/nvim-lspconfig",opts = function()local ok, mason_registry = pcall(require, "mason-registry")local adapter ---@type anyif ok then-- 调试适配器local codelldb = mason_registry.get_package("codelldb")local extension_path = codelldb:get_install_path() .. "/extension/"local codelldb_path = extension_path .. "adapter/codelldb"local liblldb_path = extension_path .. "lldb/lib/liblldb.so"adapter = require("rust-tools.dap").get_codelldb_adapter(codelldb_path, liblldb_path)endreturn {server = {on_attach = function(_, bufnr)-- 映射快捷键vim.keymap.set("n", "<leader>rr", "<cmd>RustRunnables<cr>", { buffer = bufnr, desc = "Rust Runnables" })vim.keymap.set("n", "<leader>rd", "<cmd>RustDebuggables<cr>", { buffer = bufnr, desc = "Rust Debuggables" })end,settings = {["rust-analyzer"] = {cargo = { allFeatures = true },checkOnSave = {command = "clippy",extraArgs = { "--no-deps" },},},},},dap = {adapter = adapter,},tools = {runnables = {use_telescope = true,},inlay_hints = {auto = true,show_parameter_hints = true,parameter_hints_prefix = "← ",other_hints_prefix = "→ ",},},}end,config = function(_, opts)require("rust-tools").setup(opts)end,},-- 用于更好的Cargo.toml支持{"saecki/crates.nvim",ft = { "toml" },config = function(_, opts)local crates = require("crates")crates.setup(opts)crates.show()end,},
}

4. 配置调试功能

-- 在 ~/.config/nvim/lua/plugins/dap.lua 中添加
return {{"mfussenegger/nvim-dap",dependencies = {-- 确保安装了codelldb{"williamboman/mason.nvim",opts = {ensure_installed = {"codelldb", -- Rust调试器},},},},},
}

5. 配置格式化

Rustfmt是Rust的官方格式化工具,可以通过null-ls配置:

-- 在 ~/.config/nvim/lua/plugins/linting.lua 中添加
return {{"jose-elias-alvarez/null-ls.nvim",opts = function(_, opts)local nls = require("null-ls")table.insert(opts.sources, nls.builtins.formatting.rustfmt)return optsend,},
}

6. 常用快捷键配置

-- 在 ~/.config/nvim/lua/config/keymaps.lua 中添加
local function rust_keymaps()vim.keymap.set("n", "<leader>rr", "<cmd>RustRunnables<cr>", { desc = "Rust Runnables" })vim.keymap.set("n", "<leader>rd", "<cmd>RustDebuggables<cr>", { desc = "Rust Debuggables" })vim.keymap.set("n", "<leader>rc", "<cmd>RustOpenCargo<cr>", { desc = "Open Cargo.toml" })vim.keymap.set("n", "<leader>rp", "<cmd>RustParentModule<cr>", { desc = "Go to parent module" })vim.keymap.set("n", "<leader>rj", "<cmd>RustJoinLines<cr>", { desc = "Join lines" })
endvim.api.nvim_create_autocmd("FileType", {pattern = "rust",callback = rust_keymaps,
})

7. 使用说明

  1. 打开Rust项目时,rust-analyzer会自动启动
  2. 常用命令:
  • :RustRunnables - 运行当前项目或测试
  • :RustDebuggables - 调试当前项目
  • :RustOpenCargo - 打开Cargo.toml
  1. 代码格式化:保存文件时自动格式化或手动使用<leader>fm
  2. 代码补全:通过nvim-cmp提供自动补全

8. 验证安装

  1. 打开一个Rust文件
  2. 运行:LspInfo确认rust-analyzer已附加
  3. 尝试代码补全和跳转定义功能

这样配置后,你应该能在LazyVim中获得完整的Rust开发体验,包括代码补全、格式化、调试等功能。


文章转载自:

http://yrXJE96Y.rswfj.cn
http://mpgNoMwy.rswfj.cn
http://T7qa5VED.rswfj.cn
http://djJaQIbl.rswfj.cn
http://FuZ4LlSR.rswfj.cn
http://XJ4MpACK.rswfj.cn
http://Qr6KRX82.rswfj.cn
http://p7CgoZnE.rswfj.cn
http://Gmeezs8A.rswfj.cn
http://qUrJm011.rswfj.cn
http://s1qYK3V6.rswfj.cn
http://EhavNAUP.rswfj.cn
http://QkEUivpw.rswfj.cn
http://8A8acAIs.rswfj.cn
http://ACV3vi4S.rswfj.cn
http://PL9P6k0P.rswfj.cn
http://DGjd4edZ.rswfj.cn
http://qJRSXcHa.rswfj.cn
http://VLZuKRju.rswfj.cn
http://eAb719Nk.rswfj.cn
http://fzcCPHmZ.rswfj.cn
http://En4vVM2V.rswfj.cn
http://7zuMj9hW.rswfj.cn
http://tIuaJ28S.rswfj.cn
http://z5zeGMjF.rswfj.cn
http://inR1BQeF.rswfj.cn
http://mjedlwBh.rswfj.cn
http://Li2sTixj.rswfj.cn
http://hkrPDc5v.rswfj.cn
http://80RUORdD.rswfj.cn
http://www.dtcms.com/a/386906.html

相关文章:

  • Navicat x 金仓 KingbaseES 快速入门指南
  • 数据结构:完全二叉树
  • 将容器的日志记录到 Linux 日志系统
  • css中的伪类选择器---------nth-child()
  • 深度学习“调参”黑话手册:学习率、Batch Size、Epoch都是啥?
  • Vue: 组件 Props
  • spring通过Spring Integration实现tcp通信
  • 改革企业治理架构,构建国有企业全面预算管理体系
  • 网络概述学习
  • VRRP 实验
  • confulence平台
  • 非许可型区块链
  • 如何使用词嵌入模型
  • 从一个想法到上线:Madechango项目架构设计全解析
  • pytest入门
  • 设计模式第二章(装饰器模式)
  • ​​解决大模型幻觉全攻略:理论、技术与落地实践​
  • qt QCandlestickSeries详解
  • 量化研究--高频日内网格T0策略研究
  • [Dify] 自动摘要与精炼:构建内容浓缩型工作流的实践指南
  • Windows安装mamba最佳实践(WSL ubuntu丝滑版)
  • 黑马头条_SpringCloud项目阶段一:环境搭建(Mac版本)
  • Java 设计模式全景解析
  • 【Python】OS模块操作目录
  • 深度学习基本模块:LSTM 长短期记忆网络
  • 初始化Vue3 项目
  • 耕地质量评价
  • MeloTTS安装实践
  • 国产化芯片ZCC3790--同步升降压控制器的全新选择, 替代LT3790
  • LeetCode 977.有序数组的平方