Rust autocompletion on Visual Studio Code

On my previous blog post I wrote about how to install Racer. Today I’ll show you how you can enable autocompletion for Rust code you write on Visual Studio Code, the open source editor made by Microsoft that works on Windows, Linux and MacOS X, that can be extended via plugins.

Installing Rusty Code

Make sure you have installed racer before. Instructions were given in my previous blog post. Note that you might require to have rustfmt globally installed as well. So, if you don’t have it, grab it via cargo install rustfmt.

First, you’ll want to install Rusty Code, which provides Rust integration into Visual Studio Code, including support for autocompletion, among other things. It’ll use the existing suggestions engine used in the editor and provide autocompletion à la IntelliSense in the way it already works with other languages at Visual Studio Code.

Restart the editor to activate the plugin.

Configuring Rusty Code

Over time, I have found that setting up Rusty Code in Visual Studio Code can be, either something instantaneous, or something hard. The rule of thumb is that Rusty Code will execute the commands properly if they are in your PATH, and if the source code for Rust is set to RUST_SRC_PATH, as required by racer anyway. So, if racer already works on the command line, your environment variables are already set, and Visual Studio Code can catch these variables, you’ll be good to go.

Alternatively, you can configure Rusty Code on your settings file. So, if Visual Studio Code cannot pick your $PATH and your $RUST_SRC_PATH for some reason, or because you don’t want to set them, these settings come handy.

As a safety measure I usually keep this on my Visual Studio Code settings commented out. The reason is that, I know how environment variables work on MS Windows and Linux, but after 2 years I still have no f**king clue on how environment variables work on MacOS X, and the fact that they have changed how it works over the years making lots of tutorials useless, doesn’t help1. So, if suddenly things start to crash on VSCode, I know shoething has messed up and these settings can help me debug why is it not working.

  "rust.cargoPath": "/usr/local/bin/cargo",
  "rust.racerPath": "/Users/danirod/.cargo/bin/racer",
  "rust.rustfmtPath": "/Users/danirod/.cargo/bin/rustfmt",
  "rust.rustLangSrcPath": "/usr/local/src/rustc/src",
  "rust.cargoHomePath": "/Users/danirod/.cargo"

Replace /Users/danirod by a more appropiate home directory for you. I don’t know where ~/.cargo/ is located on Microsoft Windows.

  1. This has been an sponsored rant brought to you completely free of charge. Please, enjoy.