Rust
info
You can enable the extra with the :LazyExtras
command.
Plugins marked as optional will only be configured if they are installed.
Alternatively, you can add it to your lazy.nvim
imports
lua/config/lazy.lua
require("lazy").setup({
spec = {
{ "LazyVim/LazyVim", import = "lazyvim.plugins" },
{ import = "lazyvim.plugins.extras.lang.rust" },
{ import = "plugins" },
},
})
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.
nvim-cmp
Extend auto completion
- Options
- Full Spec
opts = function(_, opts)
opts.sources = opts.sources or {}
table.insert(opts.sources, { name = "crates" })
end
{
"hrsh7th/nvim-cmp",
dependencies = {
{
"Saecki/crates.nvim",
event = { "BufRead Cargo.toml" },
opts = {
completion = {
cmp = { enabled = true },
},
},
},
},
---@param opts cmp.ConfigSchema
opts = function(_, opts)
opts.sources = opts.sources or {}
table.insert(opts.sources, { name = "crates" })
end,
}
crates.nvim
- Options
- Full Spec
opts = {
completion = {
cmp = { enabled = true },
},
}
{
"Saecki/crates.nvim",
event = { "BufRead Cargo.toml" },
opts = {
completion = {
cmp = { enabled = true },
},
},
}
nvim-treesitter
Add Rust & related to treesitter
- Options
- Full Spec
opts = { ensure_installed = { "rust", "ron" } }
{
"nvim-treesitter/nvim-treesitter",
opts = { ensure_installed = { "rust", "ron" } },
}
rustaceanvim
- Options
- Full Spec
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.
checkOnSave = true,
procMacro = {
enable = true,
ignored = {
["async-trait"] = { "async_trait" },
["napi-derive"] = { "napi" },
["async-recursion"] = { "async_recursion" },
},
},
},
},
},
}
{
"mrcjkb/rustaceanvim",
version = vim.fn.has("nvim-0.10.0") == 0 and "^4" or false,
ft = { "rust" },
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.
checkOnSave = true,
procMacro = {
enable = true,
ignored = {
["async-trait"] = { "async_trait" },
["napi-derive"] = { "napi" },
["async-recursion"] = { "async_recursion" },
},
},
},
},
},
},
config = function(_, opts)
vim.g.rustaceanvim = vim.tbl_deep_extend("keep", vim.g.rustaceanvim or {}, opts or {})
if vim.fn.executable("rust-analyzer") == 0 then
LazyVim.error(
"**rust-analyzer** not found in PATH, please install it.\nhttps://rust-analyzer.github.io/",
{ title = "rustaceanvim" }
)
end
end,
}
nvim-lspconfig
Correctly setup lspconfig for Rust 🚀
- Options
- Full Spec
opts = {
servers = {
taplo = {
keys = {
{
"K",
function()
if vim.fn.expand("%:t") == "Cargo.toml" and require("crates").popup_available() then
require("crates").show_popup()
else
vim.lsp.buf.hover()
end
end,
desc = "Show Crate Documentation",
},
},
},
},
}
{
"neovim/nvim-lspconfig",
opts = {
servers = {
taplo = {
keys = {
{
"K",
function()
if vim.fn.expand("%:t") == "Cargo.toml" and require("crates").popup_available() then
require("crates").show_popup()
else
vim.lsp.buf.hover()
end
end,
desc = "Show Crate Documentation",
},
},
},
},
},
}
mason.nvim (optional)
Ensure Rust debugger is installed
- Options
- Full Spec
opts = { ensure_installed = { "codelldb" } }
{
"williamboman/mason.nvim",
optional = true,
opts = { ensure_installed = { "codelldb" } },
}
neotest (optional)
- Options
- Full Spec
opts = {
adapters = {
["rustaceanvim.neotest"] = {},
},
}
{
"nvim-neotest/neotest",
optional = true,
opts = {
adapters = {
["rustaceanvim.neotest"] = {},
},
},
}