Setup neovim (0.5) for Rust

Hi, I know that this question was answered before, but I want to reopen it for the actual development version of nvim.

I don't want just to install vim plugins in nvim, I want to make use of the new features of nvim 0.5 as far as possible, which means init.lua files instead of init.vim and native lsp instead of coc.

I want to set up autoformatting, clippy, any other useful feature and finally rust analyzer.

The reason is performence, because the lua scripts are much faster than vim scripts. My main machine would be powerful enough, but I also use some older hardware. The other reason is: why not?

But I need a bit help because it is hard to find informations.

Any tipps or links for this?

Regards

See Neovim and Rust :: sharksforarms and GitHub - simrat39/rust-tools.nvim: Tools for better development in rust using neovim's builtin lsp

I will try rust-tools.nvim first. But both only add support for rust analyzer as far as I can see. What about rustfmt or clippy?

I've been using nvim-lspconfig (referred to in the sharksforarms link above) and it supports rustfmt. I have this in my init.vim:

nnoremap <silent> <space>f <cmd>lua vim.lsp.buf.formatting()<CR>

I don't know about clippy, though.

You can configure rust-analyzer to run clippy by setting

"rust-analyzer.checkOnSave.command": "clippy"

In the json config.

Thanks for the tip! I've only seen json configs used with vscode so far. Is there a neovim setup that uses json?

In any case, I got it working with the lua config that I've been using. Here's the relevant part:

require'lspconfig'.rust_analyzer.setup {
  on_attach = on_attach,
  settings = {
    ["rust-analyzer"] = {
      assist = {
        importMergeBehavior = "last",
        importPrefix = "by_self",
      },
      diagnostics = {
        disabled = { "unresolved-import" }
      },
      cargo = {
          loadOutDirsFromCheck = true
      },
      procMacro = {
          enable = true
      },
      checkOnSave = {
          command = "clippy"
      },
    }
  }
}

It's great to have clippy in there now.

That's the way to go! :grinning:

Found a nice repo to start with