Skip to main content

Rust

info

You can enable the extra with the :LazyExtras command. Plugins marked as optional will only be configured if they are installed.

Options

Additional options for this extra can be configured in your lua/config/options.lua file:

lua/config/options.lua
-- LSP Server to use for Rust.
-- Set to "bacon-ls" to use bacon-ls instead of rust-analyzer.
-- only for diagnostics. The rest of LSP support will still be
-- provided by rust-analyzer.
vim.g.lazyvim_rust_diagnostics = "rust-analyzer"

Below you can find a list of included plugins and their default settings.

caution

You don't need to copy the default settings to your config. They are only shown here for reference.

crates.nvim

LSP for Cargo.toml

opts = {
completion = {
crates = {
enabled = true,
},
},
lsp = {
enabled = true,
actions = true,
completion = true,
hover = true,
},
}

nvim-treesitter

Add Rust & related to treesitter

opts = { ensure_installed = { "rust", "ron" } }

rustaceanvim

opts = {
server = {
on_attach = function(_, bufnr)
vim.keymap.set("n", "<leader>cR", function()
vim.cmd.RustLsp("codeAction")
end, { desc = "Code Action", buffer = bufnr })
vim.keymap.set("n", "<leader>dr", function()
vim.cmd.RustLsp("debuggables")
end, { desc = "Rust Debuggables", buffer = bufnr })
end,
default_settings = {
-- rust-analyzer language server configuration
["rust-analyzer"] = {
cargo = {
allFeatures = true,
loadOutDirsFromCheck = true,
buildScripts = {
enable = true,
},
},
-- Add clippy lints for Rust if using rust-analyzer
checkOnSave = diagnostics == "rust-analyzer",
-- Enable diagnostics if using rust-analyzer
diagnostics = {
enable = diagnostics == "rust-analyzer",
},
procMacro = {
enable = true,
ignored = {
["async-trait"] = { "async_trait" },
["napi-derive"] = { "napi" },
["async-recursion"] = { "async_recursion" },
},
},
files = {
excludeDirs = {
".direnv",
".git",
".github",
".gitlab",
"bin",
"node_modules",
"target",
"venv",
".venv",
},
},
},
},
},
}

nvim-lspconfig

Correctly setup lspconfig for Rust 🚀

opts = {
servers = {
bacon_ls = {
enabled = diagnostics == "bacon-ls",
},
rust_analyzer = { enabled = false },
},
}

mason.nvim (optional)

Ensure Rust debugger is installed

opts = function(_, opts)
opts.ensure_installed = opts.ensure_installed or {}
vim.list_extend(opts.ensure_installed, { "codelldb" })
if diagnostics == "bacon-ls" then
vim.list_extend(opts.ensure_installed, { "bacon" })
end
end

neotest (optional)

opts = {
adapters = {
["rustaceanvim.neotest"] = {},
},
}