danirod

Installing Racer for Rust

Racer is an awesome program for enabling autocomplete for your Rust code. Rust is one of the most awesome languages I have seen. Its standard library is very impressive and the crates help extending the platform to do lots of things. However, having autocomplete comes useful for making things easier to write as you can see the functions and classes inside your editor as you are writing.

Install Racer

If you have Rust installed, this step is easy. Open your command line and install racer globally using cargo:

$ cargo install racer

Don’t take a break as it is installing racer. Let’s use this time to download and set up the source code that Racer requires in order to work properly.

Download Rust source code

Racer needs to have access to the Rust source code in order to work. I haven’t found the exact reason yet (ping me if you know), but I guess it has to do with parsing the standard library. Getting the source code is easy and fortunately you just have to download it and extract it somewhere. Get the source code from their website and place it at a folder where it’s accessible:

$ mkdir -p /usr/local/src
$ cd /usr/local/src
$ curl https://static.rust-lang.org/dist/rustc-1.11.0-src.tar.gz | tar -xf -

Of course I’m downloading Rust 1.11.0 because it’s the up to date version at the moment of writing this post. Make sure you download the same version as the one you have installed. One of the things that I like to do is to create a symlink without the version number. Then, if a new Rust version is released, I only have to change what the symlink points to without having to change anything else:

$ ln -s rustc-1.11.0 rustc  # now I use /usr/local/src/rustc

(I don’t know how will this work on Windows.)

Does Racer work?

Racer needs you to set up an environment variable. On Windows this is done via control panel (I think so). On Linux, OS X and FreeBSD this can be done via your terminal and written in your .bashrc, .zshrc, .profile or similar.

$ export RUST_SRC_PATH=/usr/local/src/rustc/src

Then let’s try to give a quick try to Racer to see if it works properly. The docs suggest using the following command, although you can try any snippet from the standard library.

$ racer complete std::io::B
MATCH BufReader,48,11,/usr/local/src/rustc/src/libstd/io/buffered.rs,Struct,pub struct BufReader<R>
MATCH BufWriter,300,11,/usr/local/src/rustc/src/libstd/io/buffered.rs,Struct,pub struct BufWriter<W: Write>
MATCH BufRead,1199,10,/usr/local/src/rustc/src/libstd/io/mod.rs,Trait,pub trait BufRead: Read
MATCH Bytes,1532,11,/usr/local/src/rustc/src/libstd/io/mod.rs,Struct,pub struct Bytes<R>

If you see crazy stuff there, like the one you see in my example, it works. Then, you are ready to go! Which editor to use is up to you. There are plugins for using Racer with a lot of editors and IDEs: Vim, Emacs, Visual Studio Code, Gedit and even Eclipse! There is more information on the README for Racer, but I’ll provide you more information for Vim and Visual Studio Code, which are the two editors I use.