Rustup beta was released this week. As a member of the Chrurch 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:
λ 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:
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:
(add-hook 'rust-mode-hook 'cargo-minor-mode)
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
λ cargo install rustfmt
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)))
ì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 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 email@example.com:rust-lang/rust.git
To use racer within emacs, do a
M-x package-install emacs-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)
emacs-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:
(add-hook 'flycheck-mode-hook #'flycheck-rust-setup)
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!