Fancy Rust development with Emacs 2016-05-14 15:10:00
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.
Installing Rust
Thanks to rustup
installing Rust on your system is now even easier. First do:
λ curl https://sh.rustup.rs -sSf | sh
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:
export PATH="$HOME/.cargo/bin:$PATH"
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
rust-mode
handles syntax highlighting, indentation and various settings for you.
Just M-x package-install rust-mode
.
cargo.el
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-b
to runcargo build
C-c C-c C-r
to runcargo run
C-c C-c C-t
to runcargo test
and many others.
As for installation, nothing fancy just M-x package-install cargo
and add:
(add-hook 'rust-mode-hook 'cargo-minor-mode)
rustfmt
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 rustfmt
crate:
λ cargo install rustfmt
I use 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:
(add-hook 'rust-mode-hook
(lambda ()
(local-set-key (kbd "C-c <tab>") #'rust-format-buffer)))
The ìndent-buffer
function I mentioned if you ever need it:
(defun indent-buffer ()
"Indent current buffer according to major mode."
(interactive)
(indent-region (point-min) (point-max)))
racer
Racer is a code completion and source code navigation tool for Rust.
Install the racer crate:
λ cargo install racer
Rust source code is needed for auto-completion so clone it somewhere:
λ git clone git@github.com:rust-lang/rust.git
To use racer within emacs, do a M-x package-install racer
and set it up like:
(setq racer-cmd "~/.cargo/bin/racer") ;; Rustup binaries PATH
(setq racer-rust-src-path "/Users/julien/Code/rust/src") ;; Rust source code PATH
(add-hook 'rust-mode-hook #'racer-mode)
(add-hook 'racer-mode-hook #'eldoc-mode)
(add-hook 'racer-mode-hook #'company-mode)
Note that racer
relies on company-mode so install it if you haven’t.
flycheck-rust
flycheck is my prefered solution for on the fly syntax checking. It will compile your code in background and highlight the problematic parts.
Let’s M-x package-install flycheck-rust
and then set it up like:
(add-hook 'flycheck-mode-hook #'flycheck-rust-setup)
Wrapping up
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!