好的!接下来是 Rust 环境


🦀 Rust 环境专用配置

目录结构

~/.config/nvim/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",
            },
            diagnostics = {
              enable = true,
              disabled = { "unresolved-proc-macro" },
            },
            inlayHints = {
              enable = true,
              typeHints = true,
              parameterHints = true,
              chainingHints = true,
            },
          }
        }
      })
    end,
  },
  
  -- 🛠️ Rust 工具 (rust-tools.nvim)
  {
    "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 = "",
          },
          hover_actions = {
            border = "rounded",
          },
        },
      })
    end,
  },
  
  -- 🐞 调试 (codelldb)
  {
    "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
        print("Installing codelldb... This may take a moment.")
        vim.fn.system({
          "git", "clone", "--depth", "1",
          "https://github.com/vadimcn/vscode-lldb.git",
          codelldb_path
        })
      end
      
      -- 配置 codelldb 适配器
      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",
      "nvim-lua/plenary.nvim",
    },
    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 环境依赖安装

# 安装 Rust 工具链
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
 
# 安装必要组件
rustup component add rust-src rust-analyzer clippy rustfmt
 
# 验证安装
rustc --version
cargo --version
rust-analyzer --version

💡 注意:如果 rust-analyzer 命令不存在,可以单独安装:

# macOS
brew install rust-analyzer
# 或通过 cargo
cargo install rust-analyzer

✅ Rust 环境验证清单

  • 切换环境:echo "rust" > ~/.config/nvim/.nvim-env
  • 启动 Neovim:nvim src/main.rs
  • LSP: <leader>gd 跳转定义,查看 inlay hints
  • 调试: <F5> 启动调试(需要先编译可执行文件)
  • 测试: <leader>tn 运行 cargo test
  • 格式化: 保存文件自动运行 rustfmt
  • 代码质量: 保存后显示 clippy 警告
  • Rust Tools: 悬停查看类型信息,查看实现关系

最后是 Writer 环境(Markdown/LaTeX 写作),准备好了吗?