Setting up a Rust development environment

Rust has decent support for most code editors out there, whether it be vim, Emacs, intellij IDE, Sublime, Atom, or Visual Studio Code. Cargo is also well supported by these editors, and the ecosystem has several tools that enhance the experience, such as the following:

  • rustfmt: It formats code according to conventions that are mentioned in the Rust style guide.
  • clippy: This warns you of common mistakes and potential code smells. Clippy relies on compiler plugins that are marked as unstable, so it is available with nightly Rust only. With rustup, you can switch to nightly easily.
  • racer: It can do lookups into Rust standard libraries and provides code completion and tool tips.

Among the aforementioned editors, the most mature IDE experience is provided by Intellij IDE and Visual Studio Code (vscode). We will cover setting up the development environment for vscode in this chapter as it is more accessible and lightweight. For vscode, the Rust community has an extension known as rls-vscode, which we'll install here. This extension is consists of the Rust language server (RLS), which uses many of the tools that we listed previously internally. We will be setting it up on Visual Studio Code 1.23.1 (d0182c341) with Ubuntu 16.04.

Installing vscode is beyond the scope of this book. You may look for your operating system's package repositories and go to https://code.visualstudio.com for more information on the same.

Let's actually open our imgtool application we created at the start of this chapter in vscode:

cd imgtool
code . # opens the current directory in vscode

Once we open our project, vscode recognizes our project automatically as a Rust project and gives us recommendations to download the vscode extension. It will look something like this:

 If you don't get recommendations, you can always type Rust in the search bar on the top left. We can then click on Install and press Reload on the extension page, which restarts vscode and makes it available for use in our project:

Next time you open the main.rs file in the project and start typing, the extension will kick in and prompt you to install any missing toolchains related to Rust that you can click install. It then starts downloading that toolchain:

After a few minutes, the status will change, like so:

Now, we are good to go.

Note: Since RLS is still in its preview phase, you may experience RLS getting stuck when installing on its first go. By restarting vscode and reinstalling RLS again after removing it, it should work. If it doesn't, feel free to raise an issue on its GitHub page: rls-vscode.

With our imgtool project opened, let's see how RLS responds when we try to import a module:

As you can see, it performs auto completion for the items that are available in the fs module in the Rust standard library. Finally, let's take a look at how RLS handles formatting code for us. Let's put all of the code on the same line to demonstrate this:

Let's save the file. We can then use Ctrl + Shift + I or Ctrl + Shift + P and select Format Document. This will instantly format the document and run cargo check against your code once you hit Save:

For more information on other code editors, Rust has a page called https://areweideyet.com that lists the status of all editors, along with the categories, showing the extent of support they have for the language. Do check them out!

Now, let's continue to implement our imgtool application.