Rustup beta was released this week. As a member of the Church of Emacs I needed to update my config a bit. This inspired me to write this post on how to setup your Emacs as a great Rust IDE.
rustup installing Rust on your system is now even easier. First do:
When prompted choose
1) Proceed with installation (default) unless you have some special requirements.
rustup was previously named
multirust. It will create a
~/.multirust directory to store rust toolchains.
The binaries and tools will be installed in
~/.cargo. Make sure that you have
~/.cargo/bin in your PATH otherwise add it with:
By default your projects will use the stable toolchain (Rust 1.8 at the time of this writing).
rustup has a lot of functionalities which you can find more about on the github page.
Crates and packages you’ll need
For this part I assume you already have a working emacs and that you know how to install packages and organize your configuration.
rust-mode handles syntax highlighting, indentation and various settings for you.
M-x package-install rust-mode.
Cargo is Rust package manager.
cargo.el is a minor mode which allows us to run cargo commands from emacs like:
C-c C-c C-bto run
C-c C-c C-rto run
C-c C-c C-tto run
and many others.
As for installation, nothing fancy just
M-x package-install cargo and add:
rustfmt formats your code according to community style guidelines just like
gofmt in the Go language world. It’s automatically handled by rust-mode.
First install the
C-c <tab> a lot in my emacs to automatically indent the current buffer so it would be nice if the same key combination would run
rustfmt which also fixes indentation. So I simply added:
ìndent-buffer function I mentioned if you ever need it:
Racer is a code completion and source code navigation tool for Rust.
Install the racer crate:
Rust source code is needed for auto-completion so clone it somewhere:
To use racer within emacs, do a
M-x package-install racer and set it up like:
racer relies on company-mode so install it if you haven’t.
flycheck is my prefered solution for on the fly syntax checking. It will compile your code in background and highlight the problematic parts.
M-x package-install flycheck-rust and then set it up like:
That’s all, you now have some great Rust support in your emacs with syntax highlighting, cargo handling, code formatting, errors highlighting, code-completion and source navigation. Happy Rust hacking!