Neovim + Rust = ❤️: How to set up the ultimate coding environment

Neovim + Rust = ❤️: How to set up the ultimate coding environment

Installing tools

To set up our Rust + Neovim development environment, we need to install some necessary tools. First, we need to install Rust itself, which can be done by following the instructions at https://www.rust-lang.org/tools/install. Next, we need to install Neovim, which is a modern and extensible version of vim. We can install Neovim from https://neovim.io/ or use our package manager of choice. Finally, we need to install some plugins for Neovim that will enhance our Rust coding experience.

Rust Essentials

Cargo is a tool that helps you manage Rust projects. Rustup is a tool that helps you install and update Rust toolchains. To install both Cargo and Rustup, you can follow the instructions at https://www.rust-lang.org/tools/install. After installing them, you can check their versions by running the following commands in your terminal:

cargo --version

rustup --version

Neovim Setup

If you are looking for a fast and easy way to set up Neovim with a lot of features and plugins, you might want to try NvChad or AstroNvim. These are two popular nvim frameworks that provide a ready-to-use configuration with a nice UI and many functionalities. You can customize them to your liking and enjoy a smooth editing experience. On the other hand, if you prefer to have more control and flexibility over your Neovim setup, you might want to go the self-route way and configure Neovim on your own. This way, you can choose exactly what plugins and settings you want and learn more about how Neovim works. It might take more time and effort, but you will have a unique and personalized configuration that suits your needs and preferences.

I prefer AstroNvim, but you can choose any one, upon making up your mind you can install them by visiting these sites:

NvChad

AstroNvim

Note: Installing Node.js[https://nodejs.org/en] is a must for setting up Auto-completion.

Configuring Neovim

Whether you've chosen AstroNvim or NvChad, setting up a Rust environment is pretty easy, just follow these steps:

Launch nvim and enter the Mason command, like this-

:Mason

It'll open a mini-window in your nvim, in the window scroll down using the "j" key on your keyboard and press "i" on the rust-analyzer, it will install the rust-analyzer for your nvim.

Now press 5 to go to the Formatter page and scroll down there you will find the Rustfmt plugin press "i" to install that and as they finish installing you have your own nvim based Rust development environment set up.

Tips

If you work on nvim a lot you'll find yourself struggling by pressing the escape key a lot, as a quick workaround you can swap your CapsLock key with your Escape key by entering this command in your Linux terminal:

setxkbmap -option caps:swapescape

Note: Not supported in Wayland sessions, If you're on a Wayland session just log out and select X11 session from the drop-down list of Desktop profiles on the Login screen.

Testing our setup

Now with everything set up, we should test it, for that, create a new rust project by entering this command in the terminal-

cargo new test_setup --bin

now go to the newly created directory by doing

cd test_setup

The main.rs file is created inside the src folder we can open that file with Neovim by entering this command:

nvim src/main.rs

Now remove the ";" from the print macro line and save it,

fn main() {
    println!("Hello, world!")
}

You should see a red warning about a missing SEMICOLON.

Conclusion

In this blog, we have learned how to set up a Rust code environment with Neovim. We have installed the necessary tools and plugins, configured the settings and keybindings, and tested our setup with a simple rust project. We have also seen some of the benefits of using Neovim for Rust development, such as fast and smart completion, syntax highlighting, error checking, formatting, debugging, and more. We hope this guide has helped you to get started with rust and Neovim, and that you enjoy coding in this powerful and versatile editor.

Did you find this article valuable?

Support Cyan Cipher by becoming a sponsor. Any amount is appreciated!