A working Rust Environment with NeoVIM

Posted on Jun 28, 2019

This is a short writeup on how to setup a Rust Environment in neovim that provides:

  • Autocompletion

  • Live-linting

  • Autoformatting

  • Syntax-highlighting

Most of the documentation I used referred to vim8 as well but I haven’t tested that.

Installing the rust toolchain(s)

First, we need to install the rust installer rustup. On Arch-Linux, rustup is packaged within the standard repositories. For other Operating Systems just follow the rustup installation guide.

Don’t forget add ~/.cargo/bin to your PATH variable.

Having the installer ready, we need to install the toolchains, sources and docs.
I want to use the stable toolchain to write code, but some of the development tools are only available for the nightly toolchain so we’re simply going to install them both.

rustup toolchain install stable
rustup toolchain install nightly
rustup default stable
rustup component add rust-src

Installing the Rust Language Server

RLS provides a backend for IDEs/editors to support their features for Rust. It is advised by it’s documentation to install it from the nightly toolchain. We’ll use rustup to install it via

rustup component add rls --toolchain nightly

In my case this command failed with a message telling me that rls is not part of the current nightly distribution. It also pointed me to have a look at this site to find the last nightly build containing it.
There I found out which nightly-YYYY-MM-DD toolchain I need to use, so I simply installed it.

rustup toolchain install nightly-YYYY-MM-DD

Now I could install rls via:

rustup component add rls --toolchain nightly-YYYY-MM-DD

Installing racer

Racer is a tool providing autocompletion for rust’s standard library.
We can simply install it via:

cargo +nightly install racer

Afterwards we have to set the RUST_SRC_PATH variable to

Installing rustfmt

rustfmt performs autoformatting on rust-code. At first I installed just rustfmt via cargo. While using it, it was showing me lots of deprication-warnings so I installed rustfmt-nightly via:

cargo install rustfmt-nightly

Installing NeoVIM plugins

There are some vim/neovim plugins needed to integrate all those tools into neovim. I won’t go into details on how to install plugins on your neovim-setup since everyone has one’s own preferences about this. Instead I’ll just list them here:

Setting up ALE

ALE is a linting-engin for neovim that implements a language-server client. To comfigure it to use the rls-server we previously installed, put the following lines into your init.vim:

let g:ale_fixers = {
      \   'rust': ['rustfmt'],

let g:ale_linters = {
      \'rust': ['rls'],

let g:ale_rust_rls_toolchain = 'nightly-YYYY-MM-DD'

let g:ale_fix_on_save = 1
let g:airline#extensions#ale#enabled = 1
nmap <silent> <C-k> <Plug>(ale_previous_wrap)
nmap <silent> <C-j> <Plug>(ale_next_wrap)

Where g:ale_rust_rls_toolchain needs to correspond to the nightly build you used to unstall rls.
This sets up ALE to use rls for linting as well as rustfmt for autoformatting.

Setting up deoplete

deoplete provides live-autocompletion in neovim. We have two sources for autocompletion installed right now; ALE and racer, so we must configure deoplete to use those two when editing rust-files.

let g:deoplete#enable_at_startup = 1
let g:deoplete#sources = {'rust': ['ale', 'racer']}

Additional Plugins

In addition to this particular rust-setup I also use some more plugins that help me writing code in neovim that might be worth a look.

I won’t go too much into details about how to set them up, just read the documentation and you’re good to go.

Tags: vimrust