BlogHome

Using Neovim for Rust Development

2022-10-12

If you currently use Vim1 or its "modern"2 iteration Neovim, or maybe
you've always wanted to check it out because you once read that all the cool-kids ™ use it and you want to write Rust code in an editor that is not VSCode. In this post, I'll share some notes on how to setup your machine3 so that you can start hacking4 some Rust code and look mighty cool doing it.

Specifically, this guide will demonstrate how to setup Rust dev environment using Neovim on MacOS.

THINGS YOU WILL NEED

  1. Bash (or zsh or fish) terminal <- choose your poison
  2. Homebrew (or a similar package manager supported by your OS)
  3. Resourcefulness in case things don't work as expected5

INSTALL NEOVIM

  1. Open your terminal. Go to home directory.

    cd ~
    pwd // verify that you are in home directory
    
  2. Install neovim using homebrew.

    // Run these one at a time
    brew update
    brew upgrade
    brew install neovim
    
  3. Confirm that the installation was successful.

    nvim -v 
    

    img

Ok, so far so good. Let's now set it up as a proper IDE so that you can start re-implementing everything written in C/C++ in the last 50 years in Rust. We can do the setup in one of two ways - learn Lua, read the Vim and Nvim docs, and build the Lua config file by hand; OR we can cheat and use a pre-built base configuration that has sane defaults and a sensible set of plugins to start using Neovim as an IDE6.

INTRODUCING ASTRONVIM

There are quite a few dotfiles that will turn your Neovim editor into a functional IDE. I am going to talk about AstroNvim because that's what I personally use for Rust development. There is no objective reason for why I chose AstroNvim over others. I guess I like its aesthetics. 🤷🏽‍♂️ Enough rambling, onwards to installing and setting up AstroNvim.

  1. Backup your current nvim configuration (if one exists)

    mv ~/.config/nvim ~/.config/nvimbackup
    
  2. Clone the GitHub repo in ~/.config/nvim <- this is where nvim config lives

    git clone https://github.com/AstroNvim/AstroNvim ~/.config/nvim
    
  3. Run the nvim package manager to install plugins

    nvim +PackerSync
    
  4. Install Rust analyzer using LspInstall

    nvim . // open nvim
    

    You should now have a blank .nvim file open in front of you.

    // Pres <Esc> to enter normal mode, then type the following
    :LspInstall rust
    

    Follow the on-screen instructions. Next, install tree-sitter (a parser generator tool) for Rust.

    nvim . // open nvim again if you exited before
    // Press <Esc> to enter normal mode
    :TSInstall rust
    
  5. Post install cleanup. Run the following commands in normal mode.

    • Run :PackerClean to remove any disabled or unused plugins
    • Run :PackerSync to update and clean plugins
  6. Keep the config updated by occassionally running :AstroUpdate

Now you should have everything you need to start (and hopefully finish) a Rust project.

Expand to see a few screenshots of Nvim and Rust:

img

img

img

CONCLUSION

Using Nvim (or Vim) as an IDE is not a lifestyle choice for the faint hearted or for the impatient, but if you persist you will be rewarded with an incredibly productive toolkit to efficiently crank out software. Hopefully, this tutorial helped you. Happy coding!


FOOTNOTES

1

Or at minimum, you've figured out how to exit Vim without restarting your machine.

2

API-first, Lua plugins, AST-producing parsing engine...fancy fancy 💅

3

These instructions are MacOS specific, might work on a subset of Linux distros.

4

Did I say hacking? I meant copy pasting code from Stack Overflow ™

5

Google is your friend.

6

You should still consider learning Lua. It's a nifty scripting language.

This work is licensed under CC BY-NC-SA 4.0.